-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.cpp
More file actions
36 lines (26 loc) · 884 Bytes
/
Copy pathCombination Sum.cpp
File metadata and controls
36 lines (26 loc) · 884 Bytes
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
//backtracking
class Solution {
public:
vector<vector<int>> _Solution;
vector<int> one_solution;
void Find_Solution(vector<int>& candidates, int k, int target);
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
Find_Solution(candidates, 0, target);
return this->_Solution;
}
};
void Solution::Find_Solution(vector<int>& candidates, int k, int target){
int L = candidates.size();
if(target == 0) {
this->_Solution.push_back(one_solution);
return;
}
if(k == L || target < 0) return;
for(int i = k; i < candidates.size(); i++){
one_solution.push_back(candidates[i]);
Find_Solution(candidates, i, target - candidates[i]);
one_solution.pop_back();
}
return;
}