class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<Integer> output = new ArrayList<>();
backTrace(candidates,0,target,output,0);
return result;
}
public void backTrace(int[] candidates,int index,int target,List<Integer> output,int lastSum){
if(lastSum == target){
result.add(new ArrayList<>(output));
}
if(lastSum >= target || index == candidates.length)
return;
for(int i = index;i < candidates.length;i++){
int num = candidates[i];
if(lastSum + num > target)
continue;
output.add(num);
backTrace(candidates,i,target,output,lastSum + num);
output.remove(output.size() - 1);
}
}
}
leetcode39. 组合总和
最新推荐文章于 2024-09-09 19:45:49 发布