-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination-Sum-ii.cpp
More file actions
29 lines (29 loc) · 844 Bytes
/
Combination-Sum-ii.cpp
File metadata and controls
29 lines (29 loc) · 844 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
class Solution {
public:
void solve(set<vector<int>>&res, vector<int>& candidates, int target, int i,vector<int>& curr){
if(target<0){
return;
}
if(target==0){
vector<int>topush(curr);
sort(topush.begin(),topush.end());
res.insert(topush);
return;
}
for(;i<candidates.size();i++){
curr.push_back(candidates[i]);
solve(res,candidates,target-candidates[i],i+1,curr);
curr.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
set<vector<int>>res;
vector<int>curr;
solve(res,candidates,target,0,curr);
vector<vector<int>>ans;
for(auto it:res){
ans.push_back(it);
}
return ans;
}
};