-
Notifications
You must be signed in to change notification settings - Fork 258
/
combination-sum.cpp
33 lines (30 loc) · 959 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
33
// Time: O(k * n^k), k is max length of combination
// Space: O(k)
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>> combinationSum(vector<int> &num, int target) {
sort(num.begin(), num.end());
vector<vector<int>> ans;
vector<int> v;
combinationSumHelper(num, target, 0, v, ans);
return ans;
}
private:
void combinationSumHelper(vector<int>& num, int gap, int begin,
vector<int>& v,vector<vector<int>> &ans) {
if (gap == 0) {
ans.emplace_back(v);
return;
}
for (size_t i = begin; i < num.size() && num[i] <= gap; ++i) {
v.emplace_back(num[i]);
combinationSumHelper(num, gap - num[i], i, v, ans);
v.pop_back();
}
}
};