深度优先搜索(回溯)
class Solution {
public:
vector<vector<int>>ans;
vector<int>path;
vector<vector<int>> combinationSum(vector<int>& nums, int target) {
dfs(nums,0,target);
return ans;
}
void dfs(vector<int>&nums,int u,int target){
if(target==0){
ans.push_back(path);
return ;
}
if(u==nums.size())return ;
for(int i=0;nums[u]*i<=target;i++){
dfs(nums,u+1,target-nums[u]*i);
path.push_back(nums[u]);
}
for(int i=0;nums[u]*i<=target;i++){
path.pop_back();
}
}
};
//dfs