【DFS】LeetCode 77. 组合

题目链接

77. 组合

思路

DFS 模板题

代码

class Solution {
    private List<List<Integer>> result = new ArrayList<>();
    private Deque<Integer> path = new ArrayDeque<>();

    public List<List<Integer>> combine(int n, int k) {
        dfs(1, n, k);

        return result;
    }

    void dfs(int current, int n, int k){
        if(path.size() == k){
            result.add(new ArrayList<>(path));
            return;
        }

        for(int i = current; i <= n; i++){
            path.addLast(i);
            dfs(i + 1, n, k);
            path.removeLast();
        }
    }
}
posted @ 2023-03-06 14:59  Frodo1124  阅读(14)  评论(0编辑  收藏  举报