- Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcombination-sum-ii.cpp
31 lines (28 loc) · 865 Bytes
/
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
//Runtime: 9 ms
classSolution {
public:
voidsolve(vector<vector<int> > &res, vector<int>&temp, vector<int>&candidates, int target, int l)
{
if(target < 0)
return;
elseif(!target)
res.push_back(temp);
else
{
for(int i=l;i<candidates.size();i++)
{
if(i > l && candidates[i] == candidates[i-1]) continue;
temp.push_back(candidates[i]);
solve(res, temp, candidates, target-candidates[i], i+1);
temp.pop_back();
}
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int> > res;
vector<int>temp;
sort(candidates.begin(), candidates.end());
solve(res, temp, candidates, target, 0);
return res;
}
};