LeetCode OJ combine 3

Posted Black_Knight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
}

 

以上是关于LeetCode OJ combine 3的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode OJ 40. Combination Sum II

<LeetCode OJ> 77. Combinations

&lt;LeetCode OJ&gt; 77. Combinations

[leetcode]Combinations

个人记录-LeetCode 77. Combinations

个人记录-LeetCode 77. Combinations