leetcode *77. 组合

【题目】*77. 组合

给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。

示例:

输入: n = 4, k = 2
输出:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

【解题思路1】全排列 - 回溯

问题变成从n-1里选k个 的组合,加上从n-1里选k-1的组合
在这里插入图片描述

考虑剪枝,如果 n = 7, k = 4,从 5 开始搜索就已经没有意义了,这是因为:即使把 5 选上,后面的数只有 6 和 7,一共就 3 个候选数,凑不出 4 个数的组合。因此,搜索起点有上界,这个上界是n - (k - path.size()) + 1
在这里插入图片描述

class Solution {
    
    
    public List<List<Integer>> combine(int n, int k) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        if (k <= 0 || n < k) {
    
    
            return res;
        }
        List<Integer> path = new ArrayList<>();
        dfs(n, k, 1, path, res);
        return res;
    }

    private void dfs(int n, int k, int index, List<Integer> path, List<List<Integer>> res) {
    
    
        if (path.size() == k) {
    
    
            res.add(new ArrayList<>(path));
            return;
        }
        // 剪枝
        for (int i = index; i <= n - (k - path.size()) + 1; i++) {
    
    
            path.add(i);
            dfs(n, k, i + 1, path, res);
            path.remove(path.size() - 1);
        }
    }
}

【解题思路2】按每一个数选与不选来递归

可以按照每一个数选与不选画出二叉树,二叉树最多 n 层,同样可以剪枝。
在这里插入图片描述

class Solution {
    
    
    public List<List<Integer>> combine(int n, int k) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        if (k <= 0 || n < k) {
    
    
            return res;
        }

        List<Integer> path = new ArrayList<>();
        dfs(1, n, k, path, res);
        return res;
    }

    private void dfs(int begin, int n, int k, List<Integer> path, List<List<Integer>> res) {
    
    
        if (k == 0) {
    
    
            res.add(new ArrayList<>(path));
            return;
        }

        // 基础版本的递归终止条件:if (begin == n + 1),这里剪枝了
        if (begin > n - k + 1) {
    
    
            return;
        }
        // 不选当前数 begin,直接递归到下一层
        dfs(begin + 1, n, k, path, res);

        // 选当前数 begin,递归到下一层的时候 k - 1,k 表示还需要选多少个数
        path.add(begin);
        dfs(begin + 1, n, k - 1, path, res);
        // 深度优先遍历有回头的过程,因此需要撤销选择
        path.remove(path.size() - 1);
    }
}

猜你喜欢

转载自blog.csdn.net/XunCiy/article/details/108464038