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

Leetcode-1278.Palindrome Partitioning IV [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-1278.Palindrome Partitioning IVhttps://leetcode.com/problems/palindrome-partitioning-iv/description/1745. 分割回文串 IV - 力扣(LeetCode)1745. 分割回文串 IV - 给你一个字符串 s ,如果可以将它分割成三个 非空 回文子字符串,那么返回 true ,否则返回 false 。当一个字符串正着读和反着读是一模一样的,就称其为 回文字符串 。 示例 1:输入:s = "abcbdd"输出:true解释:"abcbdd" = "a" + "bcb" + "dd",三个子字符串都是回文的。示例 2:输入:s = "bcbddxy"输出:false解释:s 没办法被分割成 3 个回文子字符串。 提示: * 3 <= s.length <= 2000 * s 只包含小写英文字母。https://leetcode.cn/problems/palindrome-partitioning-iv/description/

一、题目描述

Given a string s, return true if it is possible to split the string s into three non-empty palindromic substrings. Otherwise, return false.​​​​​

A string is said to be palindrome if it the same string when reversed.

Example 1:

Input: s = "abcbdd"
Output: true
Explanation: "abcbdd" = "a" + "bcb" + "dd", and all three substrings are palindromes.

Example 2:

Input: s = "bcbddxy"
Output: false
Explanation: s cannot be split into 3 palindromes.

Constraints:

  • 3 <= s.length <= 2000
  • s​​​​​​ consists only of lowercase English letters.

二、解题思路

  • 时间复杂度:O(n^2)

  • 空间复杂度:O(n^2)。

【C++】

class Solution {
public:
    bool checkPartitioning(string s) {
        vector<vector<bool>> isPalindrome(s.size(), vector<bool>(s.size(), true));
        for (int l = s.size() - 1; l >= 0; --l) {
            for (int r = l + 1; r < s.size(); ++r) {
                isPalindrome[l][r] = (s[l] == s[r]) && isPalindrome[l + 1][r - 1];
            }
        }
        for (int ml = 1; ml < s.size() - 1; ++ml) {
            if (isPalindrome[0][ml - 1]) {
                for (int mr = ml; mr < s.size() - 1; ++mr) {
                    if (isPalindrome[ml][mr] && isPalindrome[mr + 1][s.size() - 1]) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
};

isPalindrome的优势:

  • 内存局部性更优:按行从右向左填充,每次循环访问相邻内存地址,符合CPU缓存预取机制。
  • 分支预测友好:无额外条件判断(如 length == 1),减少分支预测失败的开销。
  • 初始化效率高:直接初始化所有元素为 true,仅需覆盖非回文区域,减少冗余操作。

【Java】

class Solution {
    public boolean checkPartitioning(String s) {
        boolean[][] isPalindrome = new boolean[s.length()][s.length()];
        for (int l = s.length() - 1; l >= 0; --l) {
            for (int r = l; r < s.length(); ++r) {
                isPalindrome[l][r] = (l == r)
                    ? true
                    : (s.charAt(l) == s.charAt(r)) && (l + 1 == r || isPalindrome[l + 1][r - 1]);
            }
        }
        for (int ml = 1; ml < s.length() - 1; ++ml) {
            if (isPalindrome[0][ml - 1]) {
                for (int mr = ml; mr < s.length() - 1; ++mr) {
                    if (isPalindrome[ml][mr] && isPalindrome[mr + 1][s.length() - 1]) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

相关文章:

  • Word 小黑第18套
  • 《Operating System Concepts》阅读笔记:p309-p330
  • 开启云服务器ubuntu22.04的远程桌面,支持Windows远程连接 - 开启XRDP支持
  • TypeScript装饰器
  • An effective algorithm for peptide de novo sequencing from MS/MS spectra
  • 二分算法刷题
  • 【NLP】 4. NLP项目流程与上下文窗口大小参数的影响
  • llama-factory笔记
  • python二级复习(1)
  • 编程题-第k个语法符号(中等)
  • 在react当中利用IntersectionObserve实现下拉加载数据
  • 使用生成对抗网络(GAN)进行人脸老化生成的Python示例
  • Conda 虚拟环境创建:加不加 Python 版本的深度剖析
  • Python(最新版)集成开发环境PyCharm下载安装详细教程
  • 开源WAF雷池本地化部署与远程查看网站安全防护的详细操作指南
  • 深度解析螺栓连接计算题:原理、计算与应用
  • Helm 简介与安装
  • 【leetcode hot 100 199】二叉树的右视图
  • ModelScope推理QwQ32B
  • SpringBoot手动注册定时任务
  • 网站建设的原则有哪些/成都官网seo服务
  • 攸县网站建设/口碑好网络营销电话
  • 查询网站所有关键词排名/seo推广平台
  • 若要使用链接目标在新窗口打开/seo 论坛
  • 哪里建个人网站好/今日国际新闻最新消息
  • 老太太做受网站/百度seo优化服务项目