-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination_Sum.cpp
More file actions
36 lines (32 loc) · 1.32 KB
/
Combination_Sum.cpp
File metadata and controls
36 lines (32 loc) · 1.32 KB
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
//F(k,x)=F(k+1, x)+F(k, x-x.k) 1<=k<=n, 1<=x<=m, O(N*M*M) 投资问题,不是线性的
//O(1)
class Solution {
public:
void worker(int i, int target, vector<int> &count, vector<vector<int> >&result,
vector<int> &candidates) {
if (i == candidates.size()) {
if (target == 0) {
vector<int> tmp;
for (int i = 0; i < count.size(); i++)
for (int j = 0; j < count[i]; j++)
tmp.push_back(candidates[i]);
result.push_back(tmp);
}
return;
}
for (int j = 0; j <= target / candidates[i]; j++) {
count.push_back(j);
worker(i + 1, target - j * candidates[i], count, result, candidates);
count.pop_back();
}
}
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> count;
worker(0, target, count, result, candidates);
return result;
}
};