forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
39.cpp
56 lines (46 loc) · 1.33 KB
/
39.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > ans;
vector<int> temp;
combinationSumHelper(ans, temp, candidates, target);
return ans;
}
void combinationSumHelper(vector<vector<int> > &ans, vector<int> &temp, vector<int> &candidates, int target) {
if (target == 0) {
ans.push_back(temp);
return;
}
else if (target < 0) {
return;
}
int max = 0;
for (int k = 0; k < temp.size(); k++) {
if (temp[k] > max)
max = temp[k];
}
for (int i = 0; i < candidates.size(); i++) {
if (candidates[i] >= max) {
temp.push_back(candidates[i]);
combinationSumHelper(ans, temp, candidates, target - candidates[i]);
temp.pop_back();
}
}
}
};
int main() {
vector<int> candidates = {2, 3, 6, 7};
int target = 7;
Solution s;
vector<vector<int> > ans = s.combinationSum(candidates, target);
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans[i].size(); j++) {
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}