1.题目
2.解法
①二维数组+dfs
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
int n = candidates.length;
dfs(0, target, n, candidates, res, temp);
return res;
}
private void dfs(int start, int target, int n, int[] candidates, List<List<Integer>> res, List<Integer> temp){
if(target == 0){
res.add(new ArrayList<>(temp));
return;
}
if(target > 0){
for(int i = start; i < n; i++){
temp.add(candidates[i]);
dfs(i, target - candidates[i], n, candidates, res, temp);
temp.remove(temp.size() - 1);
}
}
}
}