29 lines
777 B
C++
29 lines
777 B
C++
|
#include "s0039_combination_sum.hpp"
|
||
|
|
||
|
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.emplace_back(combine);
|
||
|
return;
|
||
|
}
|
||
|
// 直接跳过
|
||
|
dfs(candidates, target, ans, combine, idx + 1);
|
||
|
// 选择当前数
|
||
|
if (target - candidates[idx] >= 0) {
|
||
|
combine.emplace_back(candidates[idx]);
|
||
|
dfs(candidates, target - candidates[idx], ans, combine, idx);
|
||
|
combine.pop_back();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
vector<vector<int>> Solution::combinationSum(vector<int>& candidates,
|
||
|
int target) {
|
||
|
vector<vector<int>> ans;
|
||
|
vector<int> combine;
|
||
|
dfs(candidates, target, ans, combine, 0);
|
||
|
return ans;
|
||
|
}
|