Combination Sum原题如下:
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 (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 2,3,6,7
and target 7
,
A solution set is:
[7]
[2, 2, 3]
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int>>vv;
sort(candidates.begin(),candidates.end());
vector<int>v;
combination(vv,v,candidates,0,target);
return vv;
}
void combination(vector<vector<int>>&vv,vector<int>v,vector<int>cand,int start,int target){
if(target < 0)
return;
if(target == 0){
vv.push_back(v);
return;
}
for(int i = start; i < cand.size(); i++){
if(i > 0 && cand[i] == cand[i - 1])
continue;
v.push_back(cand[i]);
combination(vv,v,cand,i,target - cand[i]);
v.pop_back();
}
}
};
之前确实遇到了好多这种题目,其做法都是相似的,回溯算法的共同特点就是逐步探索,当走不通时回退一步继续选择其它路径,回溯算法在路径选择问题中确实强大,但由于该问题是NP困难的,所以其复杂度是指数级别的。
Combination Sum II 的原题如下:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
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]
class Solution {
public:
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int>>vv;
sort(num.begin(),num.end());
vector<int>v;
combination(vv,v,num,0,target);
return vv;
}
void combination(vector<vector<int>>&vv,vector<int>v,vector<int>num,int start,int target){
if(target == 0){
vv.push_back(v);
return;
}
if(target < 0 || start >= num.size())
return;
for(int i = start; i < num.size(); i++){
v.push_back(num[i]);
combination(vv,v,num,i + 1,target - num[i]);
v.pop_back();
while(i < num.size() - 1 && num[i] == num[i + 1])
i++;
}
}
};
刚开始看到还有点儿不大理解,后来仔细推理了以下才恍然大悟,代码中一次for循环相当于以i节点为初始节点(该节点一定在组合中)寻找可能的组合,而重复元素只需在第一次出现时作为初始节点寻找即可,后边的重复元素不必以初始节点出现在组合中,所以需要在for循环后加以控制。
上述两题的关键都是去除重复元素,第一题是在递归前去重,其原因是凡是重复元素都不必在遍历中出现(可以直接略过),因此去重时的比较是前向比较,而第二题是在递归后去重,其原因是重复元素只需被递归遍历一次(即以第一个重复元素为初始节点就够了),只是在后续遍历中需要跳过,因此去重时的比较是后向比较。对回溯法的理解还需要深入。。。