首页 > 其他 > 详细

39. Combination Sum

时间:2017-09-24 09:35:58      阅读:257      评论:0      收藏:0      [点我收藏+]
public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ret=new ArrayList<List<Integer>>();
        Arrays.sort(candidates);
        generateCombination(new ArrayList<Integer>(), 0, ret, candidates, target);
        return ret;
    }
    private void generateCombination(List<Integer> list, int idx, List<List<Integer>> combs, int[] candidates, int target){
        if(target==0)
        {
            combs.add(new ArrayList<Integer>(list));
            return;
        }
        if(target<0||idx>=candidates.length)
            return;
        generateCombination(list, idx+1, combs, candidates, target);
        list.add(candidates[idx]);
        generateCombination(list, idx, combs, candidates, target-candidates[idx]);
        list.remove(list.size()-1);
    }    
}

 

39. Combination Sum

原文:http://www.cnblogs.com/asuran/p/7584309.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!