-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationSum2.cpp
More file actions
28 lines (24 loc) · 804 Bytes
/
CombinationSum2.cpp
File metadata and controls
28 lines (24 loc) · 804 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
class Solution {
public:
void combinationHelper(int idx, vector<int>& arr, int target, vector<vector<int>>& ans, vector<int> ds){
if(target==0){
ans.push_back(ds);
return;
}
for(int i=idx; i<arr.size(); i++){
if(i>idx && arr[i]==arr[i-1])continue;
if(arr[i]>target) break;
ds.push_back(arr[i]);
combinationHelper(i+1, arr, target-arr[i], ans, ds);
ds.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
// a temp storage // a ans vect
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> ds;
combinationHelper(0, candidates, target, ans, ds);
return ans;
}
};