class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
ArrayList<List<Integer>> res=new ArrayList<>();
if(candidates==null||candidates.length==0){
return res;
}
Arrays.sort(candidates);
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++){
if(i>start&&candidates[i]==candidates[i-1]){
continue;
}
list.add(candidates[i]);
helper(res,list,candidates,target-candidates[i],i+1);
list.remove(list.size()-1);
}
}
}
LeetCode精选100题——第40题——组合总和 II
最新推荐文章于 2024-07-28 14:02:29 发布