标签:ios duplicate sub leetcode code 代码 理解 main each
Given a set of candidate numbers (C) (without duplicates) 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:
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]
典型的回溯法求解。代码实现中给出了 3 中回溯的方式,都 accepted。微妙的区别应该是在递归的层数不同。对于 candidates[index] 只有两种情况,即:选择或不选择,值得注意的是如果选择的话可以多次重复选择。(可以使用状态转换图进行抽象更便于理解,重复选择实际上是在 index 状态有环,而不选择则是向 index + 1 状态的迁移)
注意:
一般的,对于回溯问题,找好递归求解的子结构,记得结束点即出口的检查,避免无限循环。在递归过程中可以思考是否可以进行剪枝。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
vector<vector<int> > combinationSum(vector<int>& candidates, int target){
vector<vector<int> > ans;
vector<int> vecCur;
sort(candidates.begin(), candidates.end());
combinationSumBacktracking0(candidates, 0, target, vecCur, ans);
//combinationSumBacktracking1(candidates, 0, target, vecCur, ans);
//combinationSumBacktracking2(candidates, 0, target, vecCur, ans);
return ans;
}
// candidates in ascending
void combinationSumBacktracking0(vector<int>& candidates, int index, int target,
vector<int>& vecCur, vector<vector<int> >& ans){
if(target < 0)
return;
if(target == 0 && !vecCur.empty()){
ans.push_back(vecCur);
return;
}
// sub-problem, for each element in candidates[index,...,n-1]
// just have two condition: choose or not
for(int i=index; i<candidates.size(); i++){
if(candidates[i] > target) // Note: candidates must in ascending order
break;
// note: not i+1, because the same repeaded number may be chosen from candidates
vecCur.push_back(candidates[i]);
combinationSumBacktracking0(candidates, i, target - candidates[i], vecCur, ans);
vecCur.pop_back();
}
}
void combinationSumBacktracking1(vector<int>& candidates, int index, int target,
vector<int>& vecCur, vector<vector<int> >& ans){
if(target < 0)
return;
if(target == 0){
if(!vecCur.empty())
ans.push_back(vecCur);
return;
}
if(index >= candidates.size())
return;
// choose candidates[index]
// Note: candidates[index] can be choose more than onece
vecCur.push_back(candidates[index]);
combinationSumBacktracking1(candidates, index, target - candidates[index], vecCur, ans);
vecCur.pop_back();
// dosen‘t choose candidates[index]
combinationSumBacktracking1(candidates, index+1, target, vecCur, ans);
}
void combinationSumBacktracking2(vector<int>& candidates, int index, int target,
vector<int>& vecCur, vector<vector<int> >& ans){
if(target < 0)
return;
if(target == 0){
if(!vecCur.empty())
ans.push_back(vecCur);
return;
}
if(index >= candidates.size())
return;
// choose candidates[index] more than times
int i = 1;
for(; i*candidates[index] <= target; i++){
vecCur.push_back(candidates[index]);
combinationSumBacktracking2(candidates, index+1, target - i*candidates[index], vecCur, ans);
}
for(int j=i-1; j>=1; j--)
vecCur.pop_back();
// don‘t choose candidates[index]
combinationSumBacktracking2(candidates, index+1, target, vecCur, ans);
}
};
int main()
{
return 0;
}
[array] leetcode - 39. Combination Sum - Medium
标签:ios duplicate sub leetcode code 代码 理解 main each
原文地址:http://www.cnblogs.com/fanling999/p/7846111.html