• 【leetcode】Combination Sum II (middle) ☆


    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums toT.

    Each number in C may only be used once in the combination.

    Note:

    • All numbers (including target) will be positive integers.
    • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
    • The solution set must not contain duplicate combinations.

    For example, given candidate set 10,1,2,7,6,1,5 and target 8
    A solution set is: 
    [1, 7] 
    [1, 2, 5] 
    [2, 6] 
    [1, 1, 6] 

    思路:

    把重复的数字看做一个整体,只能出现0-重复次数遍。 这个代码特别慢,不知道为什么 550ms

    class Solution {
    public:
        vector<vector<int> > combinationSum2(vector<int> &num, int target)  {
            vector<vector<int>> ans;
            if(num.empty())
                return ans;
    
            sort(num.begin(), num.end()); //从小到大排序
            recursion(ans, num, 0 , target);
            return ans;
        }
    
        void recursion( vector<vector<int> > &ans, vector<int> candidates, int k, int target)
        {
            static vector<int> partans;
    
            if(target == 0) //如果partans中数字的总和已经达到目标, 压入答案
            {
                ans.push_back(partans); 
                return;
            }
            if(k >= candidates.size() || target < 0)
                return;
    
    
            int num = candidates[k];
            int copy = 0;
            while(k < candidates.size() && candidates[k] == num)
            {
                k++;
                copy++;
            }
    
            recursion(ans, candidates, k, target);  //不压入当前数字
    
            for(int i = 1; i <= copy; i++)
            {
                partans.push_back(num); //压入当前数字
                recursion(ans, candidates, k , target - i * num); //后面只压入大于当前数字的数,避免重复
            }
    
            //恢复数据
            while(!partans.empty() && partans.back() == num)
            {
                partans.pop_back();
            }
    
        }
    };

    大神的13ms代码,感觉和我的差距不大,为什么速度快这么多。

    class Solution {
        vector <int> path;
        vector < vector <int> > res;
    public:
        vector<vector<int> > combinationSum2(vector<int> &num, int target) {
            sort(num.begin(), num.end());
            gen(0, target, num);
            return res;
        }
        void gen(int index, int sum, vector <int> &nums) {
            if (sum == 0) {
                res.push_back(path);
                return;
            }
    
            for (int i = index; i < nums.size(); i++) { //只压入序号大于等于i的数字 避免重复
                if (sum - nums[i] < 0) return;
                if (i && nums[i] == nums[i - 1] && index < i) continue; //每次递归相同的数字只压入一次
                path.push_back(nums[i]); //这里不需要不压入nums[i]的情况,因为循环到后面时自然就是未压入该数的情况了
                gen(i + 1, sum - nums[i], nums);
                path.pop_back();
            }
        }
    };
  • 相关阅读:
    PCLVisualizer::addSphere 运行报错解决方案
    八叉树 (转载)
    Django 迁移错误 Cannot add foreign key constraint,字段类型自动变成Bigint(20)
    Django之Model字段详解
    FineReport如何连接hadoop,hive,Impala数据库,Kerberos认证
    图片自适应容器的几种方法
    【windows】查看电池使用情况
    【C++】C++复合变量的定义及易错点
    【c++】C++自定义类注意事项
    金字塔池化Spatial Pyramid Pooling
  • 原文地址:https://www.cnblogs.com/dplearning/p/4238964.html
Copyright © 2020-2023  润新知