当前位置: 首页 > article >正文

回溯法-排列,组合

回溯法

回溯法是一类特殊优先搜索,它记录节点状态,并在搜索完成后回溯状态。

leetcode46

分析

全排列问题,可以通过交换元素的方式在同一个数组完成,节省内存空间。
为了避免重复排列,限定待交换下标大等于当前下标。
为了不影响后序交换元素,通过递归实现遍历之后,将交换的下标回溯回之前的状态。

源码

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<Integer> output = new ArrayList<>();
        for (int num : nums) {
            output.add(num);
        }
        int n = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        backtrack(output, res, 0, n);
        return res;
    }
    
    private void backtrack(List<Integer> output, List<List<Integer>> res, int first, int n) {
        if (first == n) {
            res.add(new ArrayList<>(output));
            return;
        }

        for (int i = first; i < n; i++) {
            Collections.swap(output, i, first); // 交换当前位置与之后的所有位置
            backtrack(output, res, first + 1, n);
            Collections.swap(output, i, first); // 回溯交换状态
        }
    }
}

leetcode 77

分析

组合问题。排列回溯的是数组中交换的元素下标。组合回溯的是元素本身。

源码

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> temp = new ArrayList<>();

    public List<List<Integer>> combine(int n, int k) {
        dfs(1, n, k);
        return ans;
    }
    private void dfs(int cur, int n, int k) {
        
        if (temp.size() == k) {
            ans.add(new ArrayList<>(temp));
            return;
        }

        for (int i = cur; i <= n; i++) {
            temp.add(i);
            dfs(i + 1, n, k);
            temp.removeLast();
        }
    }
}

leetcode79

分析

回溯的是元素是否被访问这个状态,从而避免重复访问元素。

源码

class Solution {
    boolean found = false;
    boolean[][] visited;
    public boolean exist(char[][] board, String word) {
        int m = board.length;
        int n = board[0].length;
        visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                dfs(board, word, 0, i, j);
            }
        }
        return found;
    }
    private void dfs(char[][] board, String word, int pos, int curI, int curJ) {
        if (curI < 0 || curI >= board.length || curJ < 0 || curJ >= board[0].length) {
            return;
        }
        if (visited[curI][curJ] || word.charAt(pos) != board[curI][curJ] || found) {
            return;
        }
        if (pos == word.length() - 1) {
            found = true;
            return;
        }
        visited[curI][curJ] = true;
        dfs(board, word, pos + 1, curI + 1, curJ);
        dfs(board, word, pos + 1, curI - 1, curJ);
        dfs(board, word, pos + 1, curI, curJ + 1);
        dfs(board, word, pos + 1, curI, curJ - 1);
        visited[curI][curJ] = false;
    }
}

http://www.kler.cn/a/532412.html

相关文章:

  • 音视频入门基础:RTP专题(8)——使用Wireshark分析RTP
  • 在 Ubuntu 中使用 FastAPI 创建一个简单的 Web 应用程序
  • 031.关于后续更新和指纹浏览器成品
  • 【Pytorch和Keras】使用transformer库进行图像分类
  • 海外问卷调查渠道查,如何影响企业的运营
  • wax到底是什么意思
  • llama.cpp GGML Quantization Type
  • 5.角色基础移动
  • 云夹:重新定义你的书签管理体验
  • [mmdetection]fast-rcnn模型训练自己的数据集的详细教程
  • concurrentHasMap为什么不允许kv为null
  • 使用线性回归模型逼近目标模型 | PyTorch 深度学习实战
  • Go方法接收者中值类型接收者和指针类型接收者的对比
  • STM32F103ZET6完整技术点(持续更新~)
  • 信息安全专业2025最新毕业设计选题汇总:课题精选
  • gltf工具
  • 游戏引擎学习第86天
  • 体系自适应的物联网漏洞挖掘系统研究背景及意义:物联网漏洞挖掘概述之物联网漏洞挖掘技术前景
  • 前端开发中的“原生模块化”——深入解析ES模块(ESM)的使用与优化
  • DeepSeek 阐述 2025年前端发展趋势
  • [FPGA] MIPS 12条整数指令【2】
  • 解决 Pandas DataFrame 索引错误:KeyError:0
  • gesp(C++六级)(11)洛谷:P11246:[GESP202409 六级] 小杨和整数拆分
  • Android ExpandableListView 详细用法全解析
  • 调用高德地图 api 开发地图组件
  • FPGA 时钟多路复用