Skip to content

Commit 72d7ad6

Browse files
committed
40. Combination Sum II
1 parent a3cc51a commit 72d7ad6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

combination-sum-ii.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Runtime: 9 ms
2+
class Solution {
3+
public:
4+
5+
void solve(vector<vector<int> > &res, vector<int>&temp, vector<int>&candidates, int target, int l)
6+
{
7+
if(target < 0)
8+
return;
9+
else if(!target)
10+
res.push_back(temp);
11+
else
12+
{
13+
for(int i=l;i<candidates.size();i++)
14+
{
15+
if(i > l && candidates[i] == candidates[i-1]) continue;
16+
temp.push_back(candidates[i]);
17+
solve(res, temp, candidates, target-candidates[i], i+1);
18+
temp.pop_back();
19+
}
20+
}
21+
22+
}
23+
24+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
25+
vector<vector<int> > res;
26+
vector<int>temp;
27+
sort(candidates.begin(), candidates.end());
28+
solve(res, temp, candidates, target, 0);
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)