-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.cpp
More file actions
35 lines (30 loc) · 909 Bytes
/
Copy path39.cpp
File metadata and controls
35 lines (30 loc) · 909 Bytes
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
class Solution {
public:
void dfs(int i, int& sum, vector<int>& candidates, vector<int>& now, vector< vector<int> >& ret)
{
if(sum == target)
{
cout << sum << "|" << target << endl;
ret.push_back(now);
return;
}
for(int j = i; j < candidates.size() && sum + candidates[j] <= target; ++j)
{
now.push_back(candidates[j]);
sum += candidates[j];
dfs(j, sum, candidates, now, ret);
sum -= candidates[j];
now.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector< vector<int> > ret;
vector<int> now;
this->target = target;
int sum = 0;
dfs(0, sum, candidates, now, ret);
return ret;
}
int target;
};