码迷,mamicode.com
首页 > 其他好文 > 详细

组合总和3 leetcode 216

时间:2019-09-11 11:29:48      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:ons   col   com   one   clone   ati   leetcode   list   ret   

组合总和3

解题思路:递归回溯

class Solution {
    public List<List<Integer>> result = new ArrayList<List<Integer>>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<Integer> list = new ArrayList<>();
        combinationSum3(1,k,n,list);
        return result;
    }
    public void combinationSum3(int start, int k, int n, List<Integer> list) {
        if(k==0) {
            if(n==0) {
                List<Integer> newList = (List)((ArrayList)list).clone();
                result.add(newList);
            }
            return;
        }
        for(int i=start;i<10;++i) {
            list.add(i);
            combinationSum3(i+1,k-1,n-i,list);
            list.remove((Integer)i);
        }
    }
}

 

组合总和3 leetcode 216

标签:ons   col   com   one   clone   ati   leetcode   list   ret   

原文地址:https://www.cnblogs.com/erdanyang/p/11505001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!