forked from coderchen/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinationSum.cpp
31 lines (27 loc) · 890 Bytes
/
CombinationSum.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
class Solution {
private:
void solve(vector<vector<int> >& ans, vector<int>& rec, vector<int>& candidates, int target, int idx) {
if (target == 0) {
ans.push_back(rec);
return;
}
if (idx == candidates.size() || candidates[idx] > target) {
return;
}
for (int i = target / candidates[idx]; i >= 0; --i)
rec.push_back(candidates[idx]);
for (int i = target / candidates[idx]; i >= 0; --i) {
rec.pop_back();
solve(ans, rec, candidates, target - candidates[idx] * i, idx + 1);
}
}
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
sort(candidates.begin(), candidates.end());
candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());
vector<vector<int> > ans;
vector<int> record;
solve(ans, record, candidates, target, 0);
return ans;
}
};