-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCombination_Sum.cpp
88 lines (71 loc) · 1.98 KB
/
Combination_Sum.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
/*
Combination Sum
Given a set of candidate numbers (C) and a target number (T),
find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
All numbers (including target) will be positive integers.
Elements in a combination (a1, a2, … ,ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
*/
XXX
/*
#include <algorithm>
#include <vector>
using namespace std;
*/
/*
solution(candidates, target) ==> solution(candidates, target-a1) + solution(candidates-{a1}, target)
*/
void solve(vector<vector<int> >&result, vector<int> &picked, vector<int> &candidates, int from, int target)
{
int n = candidates.size();
if (n == from)
return;
if (candidates[from] > target)
return;
if (candidates[from] == target) {
vector<int> tmp = picked;
tmp.push_back(candidates[from]);
result.push_back(tmp);
return;
}
//pick the first
picked.push_back(candidates[from]);
solve(result, picked, candidates, from, target - candidates[from]);
picked.pop_back();
//don't pick the first
solve(result, picked, candidates, from + 1, target);
}
class Solution {
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(candidates.begin(), candidates.end());
vector<vector<int> >result;
vector<int> picked;
solve(result, picked, candidates, 0, target);
return result;
}
};
/*
int main(int argc, char *argv[])
{
Solution s;
int c[] = {2, 3, 7};
vector<int> vc(c, c + sizeof(c)/sizeof(int));
vector<vector<int> > result = s.combinationSum(vc, 7);
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result[i].size(); j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
*/