77. 组合

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combinations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。



    // 1,同一个值不能重复取
    // 乜有重复值
    Stack<Integer> stack = new Stack<>();
    List<List<Integer>> ret = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {

        dfs(n,k,1);
        return ret;

    }

    public void dfs(int n, int k, int index) {
        if(k==0) {
            ret.add(new ArrayList<>(stack));
            return;
        }

        for(int i=index;i<=n;i++) {
            stack.push(i);
            dfs(n,k-1,i+1);
            stack.pop();
        }
    }
posted @ 2022-02-28 14:23  一颗青菜  阅读(4)  评论(0)    收藏  举报