-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combination Sum.cpp
32 lines (28 loc) · 1015 Bytes
/
Combination Sum.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
// https://oj.leetcode.com/problems/combination-sum/
class Solution {
void do_combination(vector<int> &candidates, int &sum, int target, int begin,
vector<vector<int> > &ret, vector<int> &one_result) {
if (target == sum) {
ret.push_back(one_result);
return;
} else if (sum > target) {
return;
}
for (int i = begin; i < candidates.size(); i++) {
one_result.push_back(candidates[i]);
sum += candidates[i];
do_combination(candidates, sum, target, i, ret, one_result);
sum -= candidates[i];
one_result.pop_back();
}
}
public:
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
vector<vector<int> > ret;
vector<int> one;
int sum = 0;
sort(candidates.begin(), candidates.end());
do_combination(candidates, sum, target, 0, ret, one);
return ret;
}
};