1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
class Solution {
public:
void FindAll(vector<int>& candidates, vector<vector<int>>& results, vector<int> &result, int ¤t, int &target, int id)
{
if (current == target)
{
results.push_back(result);
return;
}
for (int i = id; i < candidates.size(); i++)
{
if (candidates[i] <= target - current )
{
result.push_back(candidates[i]);
current += candidates[i];
FindAll(candidates, results, result, current, target, i);
current -= candidates[i];
result.pop_back();
}
else
{
break;
}
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> results;
vector<int> result;
int current = 0;
FindAll(candidates, results, result, current, target, 0);
return results;
}
};
|