forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
26 lines (24 loc) · 834 Bytes
/
solution.h
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
#include <vector>
#include <set>
using std::vector;
#include <algorithm>
#include <functional>
class Solution {
public:
void dfs(const vector<int> &num, vector<vector<int>> &ret, int target, vector<int> cur, size_t start) {
if (target == 0) { ret.push_back(cur); return; }
for (auto i = start; i < num.size(); ++i)
if (i > start && num[i] == num[i-1]) continue;
else if (num.at(i) <= target) {
cur.push_back(num.at(i));
dfs(num, ret, target - num.at(i), cur, i+1);
cur.pop_back();
} else break;
}
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
vector<vector<int>> ret;
std::sort(num.begin(), num.end());
dfs(num, ret, target, vector<int>{}, 0);
return ret;
}
};