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

【Leetcode 每日一题】131. 分割回文串

问题背景

给你一个字符串 s s s,请你将分割成一些子串,使每个子串都是 回文串 。返回 s s s 所有可能的分割方案。

数据约束

  • 1 ≤ s . l e n g t h ≤ 16 1 \le s.length \le 16 1s.length16
  • s s s 仅由小写英文字母组成

解题过程

经典回溯题,将分割的过程看作选择在字符之间的哪个位置添加分隔符。

具体实现

选或不选

class Solution {
    private final List<List<String>> res = new ArrayList<>();
    private final List<String> path = new ArrayList<>();
    private String s;

    public List<List<String>> partition(String s) {
        this.s = s;
        dfs(0, 0);
        return res;
    }

    private void dfs(int i, int start) {
        // 当前遍历到的位置已经达到字符串末尾,记录答案并返回
        if (i == s.length()) {
            res.add(new ArrayList<>(path));
            return;
        }
        // 处理不在当前位置添加分隔符的情况,字符串末尾处是一定要添加的
        if (i < s.length() - 1) {
            dfs(i + 1, start);
        }
        // 在当前位置添加分隔符,判断字串是是否回文
        if (isPalindrome(start, i)) {
            // 添加答案
            path.add(s.substring(start, i + 1));
            // 从下一个位置开始新的递归过程
            dfs(i + 1, i + 1);
            // 恢复现场
            path.remove(path.size() - 1);
        }
    }

    // 判断字符串是否回文,可以当成模板来记
    private boolean isPalindrome(int left, int right) {
        while (left < right) {
            if (s.charAt(left++) != s.charAt(right--)) {
                return false;
            }
        }
        return true;
    }
}

选哪一个

class Solution {
    private final List<List<String>> res = new ArrayList<>();
    private final List<String> path = new ArrayList<>();
    private String s;

    public List<List<String>> partition(String s) {
        this.s = s;
        dfs(0);
        return res;
    }

    private void dfs(int i) {
        // 当前遍历到的位置已经达到字符串末尾,记录答案并返回
        if (i == s.length()) {
            res.add(new ArrayList<>(path));
            return;
        }
        // 讨论在当前状态下,后续每个可能添加分隔符的位置
        for (int j = i; j < s.length(); j++) {
            if (isPalindrome(i, j)) {
                // 添加答案
                path.add(s.substring(i, j + 1));
                // 从下一个位置开始新的递归过程
                dfs(j + 1);
                // 恢复现场
                path.remove(path.size() - 1);
            }
        }
    }
    
    // 判断字符串是否回文,可以当成模板来记
    private boolean isPalindrome(int left, int right) {
        while (left < right) {
            if (s.charAt(left++) != s.charAt(right--)) {
                return false;
            }
        }
        return true;
    }
}
http://www.dtcms.com/a/45064.html

相关文章:

  • AI军备竞赛2025:GPT-4.5的“情商革命”、文心4.5的开源突围与Trae的代码革命
  • Spring AOP 详解:面向切面编程的核心与实践
  • 网络变压器(以太网隔离变压器)在千兆以太网中扮演着信号传输、电气隔离和抗干扰的关键角色。以下是其在不同领域的典型应用案例及作用分析:
  • coze生成的工作流,发布后,利用cmd命令行执行。可以定时发日报,周报等。让他总结你飞书里面的表格。都可以
  • Java中的泛型类 --为集合的学习做准备
  • AI时代保护自己的隐私
  • 【开发心得】SpringBoot Oauth2授权登录
  • 每日一题----------类变量
  • 靶场之路-Kioptix Level-5
  • path 路径模块
  • 【Java】Stream API
  • 数据集笔记:新加坡LTA MRT 车站出口、路灯 等位置数据集
  • 开源绝版经典小游戏合集
  • 第三十四:6.4.【v-model】
  • Kali换源
  • 【Linux高级IO】多路转接(poll epoll)
  • 第49天:Web开发-JavaEE应用SpringBoot栈模版注入ThymeleafFreemarkerVelocity
  • Oracle 认证为有哪几个技术方向
  • python中如何组织项目工程文件
  • 一文学会Volatile关键字
  • DeepSeek 开源周:第六天的“One More Thing” – DeepSeek-V3/R1 推理系统的概述
  • 【Web Cache Deception简介】
  • 将QT移植到RK3568开发板
  • HarmonyOS学习第11天:布局秘籍RelativeLayout进阶之路
  • 旁路挂载实验
  • JavaScript 数据类型和数据结构:从基础到实践
  • ASPNET Core笔试题 【面试宝典】
  • ubuntu 20.04 安装labelmg
  • MyBatis-Plus 分页查询(PageHelper)
  • Debian系统查看OS Loader、内核和init/systemd相关信息