-
Notifications
You must be signed in to change notification settings - Fork 12
/
CombinationSum.cpp
38 lines (33 loc) · 1019 Bytes
/
CombinationSum.cpp
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
class Solution {
public:
void make(vector<vector<int> >& ans, vector<int>& num, vector<int> temp, int curr, int sum, int target){
int n = num.size();
if(curr >= n){
return;
}
else if(sum == target){
ans.push_back(temp);
return;
}
else if(sum > target){
return;
}
for(int i = curr; i < n; i++){
vector<int> tempp(temp);
tempp.push_back(num[i]);
int summ = sum + num[i];
make(ans, num, tempp, i, summ, target);
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
int n = candidates.size();
vector<vector<int> > ans;
for(int i = 0; i < n; i++){
vector<int> temp;
temp.push_back(candidates[i]);
int sum = candidates[i];
make(ans, candidates, temp, i, sum, target);
}
return ans;
}
};