-
Notifications
You must be signed in to change notification settings - Fork 0
/
39-CombinationSum.cpp
63 lines (57 loc) · 1.95 KB
/
39-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
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
/*=============================================================================
# FileName: 39-CombinationSum.cpp
# Desc: AC, 32ms
# Author: Jian Huang
# Email: [email protected]
# HomePage: https://cn.linkedin.com/in/huangjian1993
# Version: 0.0.1
# LastChange: 2015-08-07 09:35:30
# History:
=============================================================================*/
#include <leetcode.h>
class Solution {
public:
void dfs(vector<int> &candidates, vector<int> ret, int index, int deta, vector<vector<int> > &result) {
int len = candidates.size();
if (deta == 0) {
result.push_back(ret);
return ;
}
if (index == len) {
return ;
}
if (deta < candidates[index]) {
return ;
}
dfs(candidates, ret, index + 1, deta, result);
int count = deta / candidates[index];
for (int i = 0; i < count; i ++) {
deta -= candidates[index];
ret.push_back(candidates[index]);
dfs(candidates, ret, index + 1, deta, result);
}
}
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > result;
vector<int> ret;
if (candidates.empty()) {
return result;
}
sort(candidates.begin(), candidates.end());
dfs(candidates, ret, 0, target, result);
return result;
}
};
int main() {
vector<int> candidates;
candidates.push_back(1);
Solution solution;
vector<vector<int> > result = solution.combinationSum(candidates, 2);
for (int i = 0; i < (int)result.size(); i ++) {
for (int j = 0; j < (int)result[i].size(); j ++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}