leetcode/src/s0039_combination_sum.cpp
2022-11-30 18:20:36 +08:00

28 lines
743 B
C++

#include "s0039_combination_sum.hpp"
void S0039::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>> S0039::combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> combine;
dfs(candidates, target, ans, combine, 0);
return ans;
}