[C++]LeetCode: 33 Combination Sum

来源:互联网 发布:斗鱼皇帝 知乎 编辑:程序博客网 时间:2024/04/28 12:16

题目:

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

The same repeated number may be chosen from C unlimited number of times.

Note:

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

For example, given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 

预备知识:

1. 回溯算法:

回溯方法的步骤如下:
     1) 定义一个解空间,它包含问题的解。
     2) 用适于搜索的方式组织该空间。
     3) 用深度优先法搜索该空间,利用限界函数避免移动到不可能产生解的子空间。


2. std::unique

ForwardIterator unique (ForwardIterator first, ForwardIterator last,                          BinaryPredicate pred);
first: 首迭代器;last: 末迭代器; pred : 比较函数,可以略写。

调用unique“删除”了相邻的重复值。给“删除”加上引号是因为unique实际上并没有删除任何元素,而是将无重复的元素复制到序列的前段,从而覆盖相邻的重复元素。unique返回的迭代器指向超出无重复的元素范围末端的下一个位置


3. std::vector::erase

iterator erase (iterator position);iterator erase (iterator first, iterator last);
erase可以删除指定位置的元素,也可以删除指定范围内的元素。


思路:先将待选元素排序,并排除重复元素;开始递归,在限制一定的条件下(限定一个回溯算法的限定范围,避免不可能的搜索结果),将剩下的元素一一加到结果集合里(按照DFS原则,一条路走不下去,撤回这一步,换条路继续试探),并且更新目标值。知道穷举所有可能的解决方案。

注意和 Combination Sum II 区别。

Attention:

1. 要先对待选集合进行排序,再删除重复的元素,这样可以避免重复元素产生重复结果的影响。

2. vetor容器需要用引用参数类型, 函数调用时。

3. 如果下层迭代条件都不符合,执行nothing, 回退到上步迭代,并且删除最后一个元素,换下一个元素进行试探。

回溯算法关键点。
             “  ivec.pop_back(); ”

复杂度: NP问题。

AC Code:

class Solution {public:    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {        //输入一组非递减数字,求所有和等于目标值的组合        //思路:回溯算法 backtracking                vector<vector<int>> ret;        if(candidates.size() == 0) return ret;        vector<int> ivec;        // 先排序 再去除输入中的重复数字(unique只能去除相邻的重复元素)。 可以去除重复元素产生重复结果的影响        std::sort (candidates.begin(), candidates.end());         //erase参数 iterator erase (iterator first, iterator last);        candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end());        /* 另一种去除重复元素并排序的方法 dedup        set<int> dedup(candidates.begin(), candidates.end());        candidates = vector<int>(dedup.begin(), dedup.end());        */        recurse(ivec, target, candidates, 0, ret);        return ret;    } private:      //此处vetor容器需要用引用参数类型。    void recurse(vector<int> &ivec, int target, vector<int> &candidates, int index, vector<vector<int>> &ret){        if(target == 0)        {            ret.push_back(ivec);            return;        }                if(candidates[index] <= target){            for(int i = index; i < candidates.size() && candidates[i] <= target; i++)            {               ivec.push_back(candidates[i]);               int newtarget = target - candidates[i];               recurse(ivec, newtarget, candidates, i, ret);               //如果下层迭代条件都不符合,执行nothing, 回退到上步迭代,并且删除最后一个元素,换下一个元素进行试探。               ivec.pop_back();            }        }    }};



0 0