LeetCode OJ combine 3

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;
    }
}

 

posted @ 2016-06-30 23:33  Black_Knight  阅读(176)  评论(0编辑  收藏  举报