首页 > 其他 > 详细

LeetCode OJ combine 3

时间:2016-07-01 01:01:15      阅读:192      评论:0      收藏:0      [点我收藏+]
public class Solution {
    public List<List<Integer>> combinationSum3(int k, int n) {
        return combination(k, n, 1);
    }
    
    public List<List<Integer>> combination(int k, int target, int start) {
        List<List<Integer>> list = new ArrayList<>();
        if(k <= 0) return list;
        
        for(int i = start; i <= 10-k; i++){
            if(i < target){
                List<List<Integer>> tlist = combination(k, target - i, i+1);
                if(tlist.size() > 0){
                    for(List<Integer> alist : tlist){
                        alist.add(0, i);
                    }
                    list.addAll(tlist);
                }
            }
            else if(i == target){
                List<Integer> tlist = new LinkedList<>();
                tlist.add(target);
                list.add(tlist);
            }
            else break;
        }
        return list;
    }
}

 

LeetCode OJ combine 3

原文:http://www.cnblogs.com/liujinhong/p/5631594.html

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