forked from Soumik-7031/SDESheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum2C++
More file actions
24 lines (24 loc) · 796 Bytes
/
combinationSum2C++
File metadata and controls
24 lines (24 loc) · 796 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
class Solution {
public:
void findCombination(int ind, int target, vector<int> &arr, vector<vector<int>> &ans, vector<int>&ds) {
if(target==0) {
ans.push_back(ds);
return;
}
for(int i = ind;i<arr.size();i++) {
if(i>ind && arr[i]==arr[i-1]) continue;
if(arr[i]>target) break;
ds.push_back(arr[i]);
findCombination(i+1, target - arr[i], arr, ans, ds);
ds.pop_back();
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> ds;
findCombination(0, target, candidates, ans, ds);
return ans;
}
};