-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
112 lines (107 loc) · 2.79 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// »ØËÝ·¨
class Solution{
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
sort(candidates.begin(), candidates.end());
vector<vector<int>> res;
vector<int> combination;
dfs(target, 0, candidates, combination, res);
return res;
}
void dfs(int target, int start, vector<int>& candidates, vector<int> &combination, vector<vector<int>> &res)
{
if(!target)
{
res.push_back(combination);
return ;
}
for(int i = start; i != candidates.size() && target >= candidates[i]; ++i)
{
combination.push_back(candidates[i]);
dfs(target - candidates[i], i, candidates, combination, res);
combination.pop_back();
}
}
};
/*
class Solution{
public:
void dfs(int target, vector<int>& candidates, int start, vector<int> temp, vector<vector<int>> &res)
{
if(target == 0)
{
res.push_back(temp);
return ;
}
for(int i = start; i < candidates.size(); ++i)
{
if(i !=0 && candidates[i] == candidates[i-1]) continue;
int n = target - candidates[i];
if(n >= 0)
{
temp.push_back(candidates[i]);
dfs(n, candidates, i, temp, res);
temp.pop_back();
}
}
return ;
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
vector<vector<int>> res;
vector<int> temp;
dfs(target, candidates, 0, temp, res);
return res;
}
};
*/
/*
class Solution {
public:
vector<vector<int>> res;
vector<int> ans;
int nums_len;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
if(candidates.empty()) return res;
nums_len = candidates.size();
dfs(0, 0, target, candidates);
return res;
}
void dfs(int start, int sum, int target, vector<int> candidates)
{
if(sum == target){
res.push_back(ans);
return ;
}
else if(sum > target)
return ;
else
{
for(int i = start; i < nums_len; ++i)
{
ans.push_back(candidates[i]);
dfs(i, sum + candidates[i], target, candidates);
ans.pop_back();
}
}
}
};
*/
int main()
{
Solution s;
vector<int> data = {2,3,5};
int target = 8;
vector<vector<int>> res;
res = s.combinationSum(data, target);
for(int i = 0; i < res.size(); ++i)
for(int j = 0; j < res[i].size(); ++j)
cout << res[i][j] << " ";
cout << endl;
return 0;
}