day24|理论基础 77. 组合

文章介绍了回溯法的概念,作为递归的一种副产品,通常用于穷举解决方案并偶尔通过剪枝提高效率。以LeetCode的77号问题“组合”为例,解释了如何使用回溯法找出所有可能的组合,并展示了如何通过剪枝优化减少搜索空间。代码示例展示了回溯模板以及在解决组合问题中的应用。
摘要由CSDN通过智能技术生成

理论基础

在这里插入图片描述

回溯法:

  1. 回溯是一种搜索方式。
  2. 回溯是递归的副产品,有递归就必有回溯。
  3. 回溯本质是穷举,偶尔有剪枝来提高效率。
  4. 回溯解决的问题都可以抽象为树形结构(n叉树,有终止条件)。
  5. 回溯法解决的都是在集合中递归查找子集,集合的大小就构成了树的宽度,递归的深度就构成了树的深度。

在这里插入图片描述

回溯模版:

void backtracking(参数) {
    if (终止条件) {
        存放结果;
        return;
    }

    for (选择:本层集合中元素(树中节点孩子的数量就是集合的大小)) {
        处理节点;
        backtracking(路径,选择列表); // 递归
        回溯,撤销处理结果
    }
}

77. 组合

题目链接:https://leetcode.com/problems/combinations

Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. You may return the answer in any order.
Input: n = 4, k = 2
Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]

思路:

  1. 多层嵌套for循环
    但是n=50,k=100就无法表示
  2. 树形结构图
    在这里插入图片描述
    n相当于树的宽度,k相当于树的深度
  3. why new ArrayList<>(path)?

The new ArrayList<>(path) statement creates a new ArrayList object and initializes it with the elements of the path list.
The reason for creating a new ArrayList is to ensure that the current state of the path list is captured and stored in the result list. If we were to directly add the reference to the path list, any subsequent modifications to the path list would affect the combinations stored in the result list.

回溯法:

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        backtracking(n, k, 1); //注意从1,【1:n】
        return result;
    }
    private void backtracking(int n, int k, int startIndex) {
        if (path.size() == k) { //终止条件
            result.add(new ArrayList<>(path)); // why new?
            return;
        }
        for (int i = startIndex; i <= n; i++) { // 横向遍历
            path.add(i);
            backtracking(n, k, i+1); //纵向,从i+1开始
            path.remove(path.size()-1); // path.removeLast();
        }
    }
}

剪枝优化:
在这里插入图片描述

  1. 已经选择的元素个数:path.size();
  2. 需要的元素个数为: k - path.size();
  3. 列表中剩余元素(n-i) >= 所需需要的元素个数(k - path.size())
  4. 在集合n中至多要从该起始位置 : i <= n - (k - path.size()) + 1,开始遍历
    如果for循环选择的起始位置之后的元素个数 已经不足 我们需要的元素个数了,那么就没有必要搜索了
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        backtracking(n, k, 1);
        return result;
    }
    private void backtracking(int n, int k, int startIndex){
        if (path.size() == k){
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i <= n-(k-path.size())+1; i++) { //剪枝操作,至多开始位置
            path.add(i);
            backtracking(n, k, i+1);
            path.remove(path.size()-1);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值