首页 > 其他 > 详细

组合总和3 leetcode 216

时间:2019-09-11 11:02:31      阅读:93      评论:0      收藏:0      [点我收藏+]

组合总和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

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

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