forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination-sum-ii_1_AC.cpp
54 lines (51 loc) · 1.44 KB
/
combination-sum-ii_1_AC.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
52
53
54
#include <unordered_map>
using std::unordered_map;
class Solution {
public:
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
vector<vector<int>> res;
int n = candidates.size();
if (n == 0) {
return res;
}
unordered_map<int, int> um;
vector<int> v, tc, c;
int i;
for (i = 0; i < n; ++i) {
++um[candidates[i]];
}
for (auto it = um.begin(); it != um.end(); ++it) {
v.push_back(it->first);
tc.push_back(it->second);
}
dfs(0, target, c, v, tc, res);
return res;
}
private:
void dfs(int sum, int target, vector<int> &c, vector<int> &v, vector<int> &tc,
vector<vector<int>> &res) {
int idx = c.size();
if (idx == tc.size()) {
if (sum == target) {
int i, j;
vector<int> a;
for (i = 0; i < idx; ++i) {
for (j = 0; j < c[i]; ++j) {
a.push_back(v[i]);
}
}
res.push_back(a);
}
return;
}
int i;
for (i = 0; i <= tc[idx]; ++i) {
if (sum + i * v[idx] > target) {
break;
}
c.push_back(i);
dfs(sum + i * v[idx], target, c, v, tc, res);
c.pop_back();
}
}
};