-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombination Sum II.cpp
43 lines (31 loc) · 1 KB
/
Combination Sum II.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
//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>> combinationSum2(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;
int i = k;
int j = i;
while( candidates[i] == candidates[j] && j < L) j++;
for(int n = 0; n <= j-i; n++){
if(n > 0)
one_solution.push_back(candidates[i]);
Find_Solution(candidates, j, target - candidates[i]*n);
}
for(int n = i; n < j; n++)
one_solution.pop_back();
return;
}