LeetCode 39. Combination Sum

来源:互联网 发布:恐怖黎明数据库 编辑:程序博客网 时间:2024/06/11 00:49
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;    }}

0 0
原创粉丝点击