-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum2_leetcode40.cpp
51 lines (50 loc) · 1.54 KB
/
combination_sum2_leetcode40.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
51
/*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 <iostream>
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void findcomb(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++)
{ // for avoiding duplicates
if (i > ind && arr[i] == arr[i - 1])
continue;
if (arr[i] > target)
break;
ds.push_back(arr[i]);
findcomb(i + 1, target - arr[i], arr, ans, ds);
ds.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int> &candidates, int target)
{
sort(candidates.begin(), candidates.end());
vector<vector<int>> ans;
vector<int> ds;
findcomb(0, target, candidates, ans, ds);
return ans;
}
};
int main()
{
vector<int> arr = {10, 1, 2, 7, 6, 1, 5};
vector<vector<int>> vec;
Solution s;
vec = s.combinationSum2(arr, 8);
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[i].size(); j++)
cout << vec[i][j] << " ";
cout << endl;
}
}