首页 > 其他 > 详细

[leetcode] 77. 组合

时间:2018-07-30 14:19:20      阅读:194      评论:0      收藏:0      [点我收藏+]

77. 组合

递归枚举搜就好

class Solution {

    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ans = new ArrayList<>();

        List<Integer> cur = new ArrayList<>();

        dfs(n, k, 0, cur, ans);
        return ans;
    }

    private void dfs(int n, int k, int last, List<Integer> cur, List<List<Integer>> ans) {
        if (k == 0) {
            ans.add(new ArrayList<>(cur));
            return;
        }
        for (Integer i = last + 1; i <= n; i++) {
            cur.add(i);
            dfs(n, k - 1, i, cur, ans);
            cur.remove(i);
        }
    }
}

[leetcode] 77. 组合

原文:https://www.cnblogs.com/acbingo/p/9390093.html

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