class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
ArrayList<List<Integer>> res=new ArrayList<>();
if(candidates==null||candidates.length==0){
return res;
}
helper(res,new ArrayList<Integer>(),candidates,target,0);
return res;
}
private void helper(ArrayList<List<Integer>> res,ArrayList<Integer> list,int[] candidates,int target,int start){
if(target==0){
res.add(new ArrayList(list));
return;
}
if(target<0){
return;
}
for(int i=start;i<candidates.length;i++){
list.add(candidates[i]);
helper(res,list,candidates,target-candidates[i],i);
list.remove(list.size()-1);
}
}
}
LeetCode精选100题——第39题——组合总和
最新推荐文章于 2022-02-28 17:36:43 发布