-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinationSum.cpp
83 lines (64 loc) · 2.1 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end(), less<int>());
for(int i = candidates.size() - 1; i != -1; i --){
if(candidates[i] <= target){
return subSum(candidates, i, target);
}
}
vector<vector<int>> emptyResult;
return emptyResult;
}
vector<vector<int>> subSum(vector<int>& candidates, int end, int target){
if(end == -1 || target < candidates[0]){
vector<vector<int>> emptyResult;
return emptyResult;
}
int i = 0;
vector<vector<int>> result;
while(target - candidates[end] * i >= 0){
vector<vector<int>> subResult = subSum(candidates, end - 1, target - candidates[end] * i);
subResult = combineResult(subResult, candidates[i], i);
int len = subResult.size();
for(int i = 0; i != len; i ++){
result.push_back(subResult[i]);
}
i ++;
}
return result;
}
vector<vector<int>> combineResult(vector<vector<int>> subResult, int combNum, int repeatTime){
if(repeatTime <= 0){
vector<vector<int>> emptyResult;
return emptyResult;
}
if(subResult.empty()){
vector<vector<int>> newResult;
vector<int> resultArr;
for(int i = 0; i != repeatTime; i ++){
resultArr.push_back(combNum);
}
newResult.push_back(resultArr);
return newResult;
}
int len = subResult.size();
for(int i = 0; i != len; i ++){
for(int j = 0; j != repeatTime; j ++){
subResult[i].push_back(combNum);
}
}
return subResult;
}
};
int main(int argc, char const *argv[]) {
vector<int> candidates;
candidates.push_back(2);
candidates.push_back(3);
candidates.push_back(6);
candidates.push_back(7);
return 0;
}