-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode40.cpp
More file actions
57 lines (30 loc) · 863 Bytes
/
leetcode40.cpp
File metadata and controls
57 lines (30 loc) · 863 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
void getallcombintion(vector<int>& candidates,int target,int idx,vector<vector<int>>&ans,vector<int>&combin){
// if(idx==candidates.size()||target<0){
// return;
//}
if(target==0){
ans.push_back(combin);
return;
}
for(int i =idx;i<candidates.size();i++){
if(i>idx&&candidates[i]==candidates[i-1]){
continue;
}
if(candidates[i]>target){
break;
}
combin.push_back(candidates[i]);
getallcombintion(candidates,target-candidates[i],i+1,ans,combin);
combin.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
vector<vector<int>>ans;
vector<int>combin;
getallcombintion(candidates,target,0,ans,combin);
return ans;
}
};