利用begin
结束条件为满足条件
就这
class Solution {
List<List<Integer>> res =new ArrayList<List<Integer>>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
if(candidates.length==0){
return res;
}
List<Integer> path=new ArrayList<>();
core(candidates,target,0,path);
return res;
}
public void core(int[] candidates,int target,int begin,List<Integer> path){
if(target<0){
return;
}
if(target==0){
res.add(new ArrayList(path));
return;
}
for(int i=begin;i<candidates.length;i++){
path.add(candidates[i]);
core(candidates,target-candidates[i],i,path);
path.remove(path.size()-1);
}
}
}