-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCombination Sum II.cpp
50 lines (36 loc) · 1.34 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Solution by Rahul Surana
***********************************************************
Given a collection of candidate numbers (candidates) and a target number (target),
find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
set<vector<int>> ans;
void df(int i, int target,vector<int>& candidates, vector<int> a){
if(target < 0) return;
if(target == 0) {
ans.insert(a);
return;
}
for(int j = i; j < candidates.size(); j++){
if(j > i && candidates[j] == candidates[j-1]) continue;
if(target - candidates[j] >= 0){
a.push_back(candidates[j]);
df(j+1,target-candidates[j], candidates,a);
a.pop_back();
}
else break;
}
}
vector<vector<int>> combinationSum2(vector<int>& c, int target) {
sort(c.begin(),c.end());
vector<int> a;
df(0,target, c, a);
return vector<vector<int>>(ans.begin(), ans.end());
}
};