题目链接
class Solution {
public:
void dfs(vector<int>& candidates,int target,vector<vector<int>>& ans,
vector<int>& combine,int idx) {
if(idx==candidates.size())return;
if(target==0) {
ans.push_back(combine);
return ;
}
dfs(candidates,target,ans,combine,idx+1);
if(target-candidates[idx]>=0) {
combine.push_back(candidates[idx]);
dfs(candidates,target-candidates[idx],ans,combine,idx);
combine.pop_back();
}
}
vector<vector<int>> combinationSum(vector<int> candidates,int target) {
vector<vector<int>> ans;
vector<int> combine;
dfs(candidates,target,ans,combine,0);
return ans;
}
};
|