class Solution {
public:
vector<vector<int>> res;
vector<int> path;
int sum = 0;
void dfs(int start, vector<int>& candidates, int target) {
if (sum > target) return;
if (sum == target) {
res.push_back(path);
return;
}
for (int i = start; i < candidates.size(); i ++) {
path.push_back(candidates[i]);
sum += candidates[i];
dfs(i, candidates, target); //i不用加1表示可以取到这个数
path.pop_back();
sum -= candidates[i];
}
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
dfs(0, candidates, target);
return res;
}
};
标签:target,19,sum,int,vector,candidates,path,LeetCode,总和
From: https://www.cnblogs.com/hjy94wo/p/16654242.html