Leetcode 39
Combination Sum
class Solution {
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> res;
vector<int> t;
sort(candidates.begin(), candidates.end());
combination(t, res, 0, candidates, target);
return res;
}
void combination(vector<int>&t, vector<vector<int>>& res, int i, vector<int>& candidates, int target) {
if (target<0) {
return;
} else if (target == 0) {
res.push_back(t);
return;
} else {
for (int j = i; j < candidates.size(); j++) {
t.push_back(candidates[j]);
combination(t, res, j, candidates, target-candidates[j]);
t.erase(t.begin()+t.size()-1);
}
}
}
};