-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0039.cpp
39 lines (30 loc) · 993 Bytes
/
0039.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
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> result;
vector<int> solution;
findSolution(candidates, target, solution, 0, 0, result);
return result;
}
void findSolution(vector<int>& candidates, int target, vector<int>& solution, int sum, int index, vector<vector<int>> &result) {
if (sum == target) {
result.push_back(solution);
return;
}
if (index == candidates.size()) {
return;
}
findSolution(candidates, target, solution, sum, index + 1, result);
// int max_count = target / candidates[index];//超时代码
int max_count = (target - sum) / candidates[index];
for (int i = 0; i < max_count; ++ i) {
solution.push_back(candidates[index]);
sum += candidates[index];
findSolution(candidates, target, solution, sum, index + 1, result);
}
while (max_count--) {
solution.pop_back();
}
}
};