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

[leetcode] 77. 组合

时间:2018-07-30 14:47:12      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:list   https   vat   solution   esc   new   last   ++   int   

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. 组合

标签:list   https   vat   solution   esc   new   last   ++   int   

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

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