public class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
if(candidates==null||candidates.length==0) return res;
Arrays.sort(candidates);
int len = candidates.length;
List<Integer> list = new ArrayList<Integer>();
dfs(list, candidates, 0, target);
return res;
}
public void dfs(List<Integer> list, int[] candidates, int sum, int target){
int len = candidates.length;
for(int i=0;i<len;++i){
if(sum + candidates[i]==target){
List<Integer> tmp = new ArrayList<Integer>(list);
tmp.add(candidates[i]);
Collections.sort(tmp);
if(!res.contains(tmp)){
res.add(tmp);
}
}else if(sum + candidates[i] < target){
List<Integer> t1 = new ArrayList<Integer>(list);
t1.add(candidates[i]);
dfs(t1, candidates, sum + candidates[i], target);
}else{
break;
}
}
}
}
39 Combination Sum
最新推荐文章于 2024-11-20 10:20:22 发布
