https://leetcode-cn.com/problems/combination-sum/
思路:dfs
void dfs(vector<int> &candidates, int value, vector<int> temp, vector<vector<int> > &res, int idx) {
if (value <= 0) {
if (value == 0) {
res.push_back(temp);
}
return;
}
//用i来控制无限制选取本数字,同时用i来控制不能选取本数字前面的数字,用来去重
for (int i = idx; i < candidates.size(); i++) {
temp.push_back(candidates[i]);
dfs(candidates, value - candidates[i], temp, res, i);
temp.pop_back();
}
}
vector<vector<int> > combinationSum(vector<int>& candidates, int target) {
vector<vector<int> > res;
if (candidates.size() == 0) {
return res;
}
vector<int> temp;
dfs(candidates, target, temp, res, 0);
return res;
}