class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
dfs(0, candidates, new LinkedList<>(), target);
return res;
}
public void dfs(int u, int[] candidates, LinkedList<Integer> list, int target) {
if (target == 0 ) {
res.add(new LinkedList<>(list));
return;
}
if(u == candidates.length) return;
for (int i = 0; i * candidates[u] <= target; i ++) {
dfs(u + 1, candidates, list, target - i * candidates[u]);
list.add(candidates[u]);
}
for (int i = 0; i * candidates[u] <= target; i ++) {
list.removeLast();
}
}
}
组合总和
最新推荐文章于 2023-09-09 12:19:44 发布