-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path组合总和 II
28 lines (28 loc) · 790 Bytes
/
组合总和 II
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
class Solution {
public:
vector<vector<int>> result;
vector<int> v;
int sum=0;
void back(vector<int> &candidates, int target, int k){
if(sum==target){
result.push_back(v);
return;
}
if(sum>target){
return;
}
for(int i=k;i<candidates.size();i++){
if(i>k&&candidates[i]==candidates[i-1]) continue;
v.push_back(candidates[i]);
sum+=candidates[i];
back(candidates, target, i+1);
v.pop_back();
sum-=candidates[i];
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
back(candidates, target, 0);
return result;
}
};