39. Combination Sum
class Solution {
private:
vector<vector<int>> res;
vector<int> comb;
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target)
{
backtracking(candidates, target, 0);
return res;
}
void backtracking(vector<int>& candidates, int target, int start)
{
if (target == 0) res.push_back(comb);
if (target < 0) return;
for (int i = start; i < candidates.size(); i++)
{
comb.push_back(candidates[i]);
backtracking(candidates, target - candidates[i], i);
comb.pop_back();
}
}
};
- T:
- S: