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