public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
return combine(candidates, 0, target);
}
public List<List<Integer>> combine(int[] candidates, int s, int k) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
for (; s < candidates.length; s++) {
if (k < candidates[s]) break;
else if (k == candidates[s]) {
List<Integer> tmpList = new ArrayList<Integer>();
tmpList.add(k);
list.add(tmpList);
break;
}
else {
List<List<Integer>> tmp = combine(candidates, s, k - candidates[s]);
if (tmp != null) {
for (List<Integer> l : tmp) {
l.add(candidates[s]);
list.add(l);
}
}
}
}
return list;
}
}
LeetCode 39. Combination Sum
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.