forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths1.cpp
26 lines (26 loc) · 770 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/combination-sum-ii/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N)
class Solution {
vector<vector<int>> ans;
void dfs(vector<int> &A, int target, int start, vector<int> &tmp) {
if (!target) {
ans.push_back(tmp);
return;
}
for (int i = start; i < A.size() && target >= A[i]; ++i) {
if (i != start && A[i] == A[i - 1]) continue;
tmp.push_back(A[i]);
dfs(A, target - A[i], i + 1, tmp);
tmp.pop_back();
}
}
public:
vector<vector<int>> combinationSum2(vector<int>& A, int target) {
sort(A.begin(), A.end());
vector<int> tmp;
dfs(A, target, 0, tmp);
return ans;
}
};