Combination Sum(组合总和)
题目:链接
代码
class Solution
{
private:
vector<vector<int>> rems;
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());
vector<int> rets;
backtracking(candidates, target, rets, 0);
return rems;
}
void backtracking(vector<int> &candidates, int target, vector<int> &rets, int start)
{
if(target == 0)
{
rems.push_back(rets);
return;
}
if(target < 0)
return;
for(int i = start; i < candidates.size(); ++i)
{
rets.push_back(candidates[i]);
backtracking(candidates, target-candidates[i], rets, i);
rets.pop_back();
}
}
};