-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinational Sum-2
More file actions
35 lines (30 loc) · 909 Bytes
/
Combinational Sum-2
File metadata and controls
35 lines (30 loc) · 909 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
29
30
31
32
33
34
35
class Solution {
public:
void solve(vector<int>&ip , int target , int sum , int ind, vector<int>&op , vector<vector<int>>&ans)
{
if(sum == target){
ans.push_back(op);
return;
}
if(ind >= ip.size())
return;
if(sum >target)
return;
for(int i = ind ; i<ip.size() ; i++)
{
if(i>ind && (ip[i] == ip[i-1]) )
continue;
op.push_back(ip[i]);
solve(ip , target , sum+ip[i] , i+1 , op , ans);
op.pop_back();
}
return;
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin() , candidates.end());
vector<vector<int>>ans;
vector<int>op;
solve(candidates , target , 0 , 0 ,op , ans );
return ans;
}
};