-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcombination-sum-ii(AC).cpp
64 lines (61 loc) · 1.42 KB
/
combination-sum-ii(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
55
56
57
58
59
60
61
62
63
64
#include <map>
using namespace std;
class Solution {
public:
/**
* @param num: Given the candidate numbers
* @param target: Given the target number
* @return: All the combinations that sum to target
*/
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
v.clear();
a.clear();
c.clear();
ans.clear();
n = num.size();
if (n == 0) {
return ans;
}
map<int, int> mm;
int i;
for (i = 0; i < n; ++i) {
++mm[num[i]];
}
for (auto it = mm.begin(); it != mm.end(); ++it) {
a.push_back(it->first);
c.push_back(it->second);
}
n = a.size();
t = target;
DFS(0, 0);
return ans;
}
private:
vector<int> a, c;
vector<int> v;
int n;
int t;
vector<vector<int> > ans;
void DFS(int idx, int sum) {
if (sum == t) {
ans.push_back(v);
return;
}
if (idx == n) {
return;
}
int i, j;
for (i = 0; i <= c[idx]; ++i) {
if (sum + i * a[idx] > t) {
break;
}
for (j = 0; j < i; ++j) {
v.push_back(a[idx]);
}
DFS(idx + 1, sum + i * a[idx]);
for (j = 0; j < i; ++j) {
v.pop_back();
}
}
}
};