-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlt0040.cpp
More file actions
46 lines (35 loc) · 1.12 KB
/
Copy pathlt0040.cpp
File metadata and controls
46 lines (35 loc) · 1.12 KB
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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
if(candidates.size() == 0)
return {};
vector<vector<int>> res = {};
vector<int> record = {};
sort(candidates.begin(), candidates.end());
helper(res, candidates, record, target , 0);
return res;
}
void helper(vector<vector<int>>& res, vector<int>& candidates, vector<int>& record, int target, int step)
{
if(target == 0)
{
res.push_back(record);
return;
}
if(target < 0)
return;
for(int i = step; i < candidates.size(); i++)
{
if(candidates[i] > target)
return;
if(i > step && candidates[i-1] == candidates[i])
continue;
record.push_back(candidates[i]);
helper(res, candidates, record, target - candidates[i], i + 1);
record.pop_back();
}
}
};