-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum2.cpp
More file actions
39 lines (36 loc) · 1.06 KB
/
combinationSum2.cpp
File metadata and controls
39 lines (36 loc) · 1.06 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
// problem: https://leetcode.com/problems/combination-sum-ii/
// Runtime: 4 ms, faster than 89.86% of C++ online submissions for Combination Sum II.
// Memory Usage: 10.7 MB, less than 51.13% of C++ online submissions for Combination Sum II.
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
void backtrack(vector<int> &candidates, vector<int> ¤t, vector<vector<int>> &answer, int target, int start)
{
if (target < 0)
return;
if (!target)
{
vector<int> copy = current;
answer.push_back(copy);
}
for (int i = start; i < candidates.size(); ++i)
{
if (i > start && candidates[i] == candidates[i - 1])
continue;
current.push_back(candidates[i]);
this->backtrack(candidates, current, answer, target - candidates[i], i + 1);
current.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int> &candidates, int target)
{
vector<vector<int>> answer;
sort(candidates.begin(), candidates.end());
vector<int> current;
this->backtrack(candidates, current, answer, target, 0);
return answer;
}
};