-
Notifications
You must be signed in to change notification settings - Fork 0
/
191209-1.cpp
63 lines (60 loc) · 1.35 KB
/
191209-1.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
// https://leetcode-cn.com/problems/combination-sum/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> results;
vector<int> counts;
process(results, candidates, counts, target);
return results;
}
private:
void process(vector<vector<int>>& results, const vector<int>& candidates,
vector<int>& counts, int target) {
if (counts.size() < candidates.size()) {
int i = counts.size();
for (int c = target / candidates[i]; c >= 0; --c) {
counts.push_back(c);
process(results, candidates, counts, target - candidates[i] * c);
counts.pop_back();
}
} else {
if (target == 0) {
vector<int> item;
for (int i = 0; i < counts.size(); ++i) {
for (int j = 0; j < counts[i]; ++j) {
item.push_back(candidates[i]);
}
}
results.push_back(item);
}
}
}
};
void print(const vector<vector<int>>& a)
{
printf("[\n");
for (const auto& e : a) {
printf(" [ ");
for (auto e2 : e) {
printf("%d ", e2);
}
printf("]\n");
}
printf("]\n");
}
int main()
{
Solution s;
{
vector<int> a = { 2, 3, 6, 7 };
print(s.combinationSum(a, 7)); // answer: [ [7], [2,2,3] ]
}
{
vector<int> a = { 2, 3, 5 };
print(s.combinationSum(a, 8)); // answer: [ [2,2,2,2], [2,3,3], [3,5] ]
}
return 0;
}