• 题目描述

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

给定一个字符串s,找出s中最长的回文子字符串。可假设s的最大长度是1000。

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"
  • 思路分析

对于回文串,大家一定都不陌生啦,即正读,反读都一样的字符串。

求解最长回文子串问题,必须是马大神的Manacher's Algorithm,时间复杂度O(n),线性搞定,完虐中心扩展和动态规划这些思路

Manacher's Algorithm思路如下:

  1. 对给定字符串进行预处理:
    ①字符两两之间和字符串头尾都插入任一特殊字符,确保处理后的字符串长度一定为奇数,不必再分情况列出奇偶两种情况。eg:abba (4)—>$a$b$b$a$(9)         cde(3)—>$c$d$e$(7)
    ②在处理后的字符串前后各加一个相异的特殊字符,防止字符越界问题
  2. 开一个数组c来存储以B中的第n个元素为中心的最大回文子串,即B[n]为中心的最大回文子串(为了方便起见,原始字符串称A,处理后的字符串称B,新数组称为c)
    eg:B $ a $ b $ b $ a $
         c  0 1 0 1 4 1 0 1 0
    从eg中,我们可以发现c[n] 即以A[n]为中心的最长回文子串长度
    所以找到回文子串的最大长度等价于:找到最大的c[n]
  3. 增加两个帮手,pos来表示最大回文子串的中心,max(=pos+c[pos])表示最大回文子串的半径。
  4. 下面进入大餐,充分利用回文串 内回文 的对称性,这里我怕说不好,贴一下大神的助攻下
    https://blog.crimx.com/2017/07/06/manachers-algorithm/
    https://articles.leetcode.com/longest-palindromic-substring-part-ii/
  • 源码附录
class Solution {
public String longestPalindrome(String s) {
if(s.length()<=1){
return s;
} StringBuilder sb = new StringBuilder();
sb.append("#"); for(int i=0;i<s.length();i++){
sb.append("$");
sb.append(s.charAt(i));
}
sb.append("$*"); int pos = 0;
int r = 0;
int mi = 0;
int ms = 0;
int[] t = new int[sb.length()]; for(int i=1;i<sb.length();i++){
int mirrorCur = 2*pos-i;
t[i] = (i>r) ? 1:(Math.min(t[mirrorCur],r-i));
while((i+t[i])<sb.length() && (sb.charAt(i+t[i]) == sb.charAt(i-t[i]))){
t[i] ++;
} if(i+t[i] > r){
r = t[i] + i;
pos = i;
}
if(t[i]>ms){
ms = t[i];
mi = i;
}
}
return s.substring((mi-ms)/2,(mi+ms)/2-1);
}
}

自行理解,实在不理解的话,参照下文详解版

class Solution {
public String longestPalindrome(String s) {
if(s.length()<=1){
return s;
} StringBuilder sb = new StringBuilder();//进行预处理
sb.append("#"); for(int i=0;i<s.length();i++){
sb.append("$");
sb.append(s.charAt(i));
}
sb.append("$*"); int pos = 0;//当前情况下能够到达的最远的回文子串中心
int r = 0;//当前情况下能到达的最远回文子串半径
int mi = 0;//记录最长的回文子串中心
int ms = 0;//记录最长的回文子串半径
int[] t = new int[sb.length()]; for(int i=1;i<sb.length();i++){
int mirrorCur = 2*pos-i;//记录当前下标和pos的对称点
/*
if(i<r){//当前下标小于当前能够到达的最远回文子串的半径时,用下标的t值
t[i] = Math.min(t[mirrorCur],r-i)
}
else{//负责置1等待
t[i] = 1;
}
*/
t[i] = (i>r) ? 1:(Math.min(t[mirrorCur],r-i)); //检查并更新以当前下标为中心的回文子串到达的最远长度
while((i+t[i])<sb.length() && (sb.charAt(i+t[i]) == sb.charAt(i-t[i]))){
t[i] ++;
} if(i+t[i] > r){ //检查并更新当前能够到达的最远的回文子串的信息
r = t[i] + i;
pos = i;
} if(t[i]>ms){ //更新当前已知的最长的回文串子信息
ms = t[i];
mi = i;
}
}
return s.substring((mi-ms)/2,(mi+ms)/2-1);//去掉辅助符号 进行输出
}
}

Longest Palindromic Substring-----LeetCode进阶路⑤的更多相关文章

  1. Longest Palindromic Substring——LeetCode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. Longest Palindromic Substring -LeetCode

    题目 Given a string s,find the longest palindromic substring in S.You may assume  that the maximum len ...

  3. Longest Palindromic Substring leetcode java

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  4. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. 求最长回文子串 - leetcode 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  7. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  8. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. [LeetCode] Longest Palindromic Substring(manacher algorithm)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  10. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

随机推荐

  1. Vue + Element 实现按钮指定间隔时间点击

    1.业务需求 需要加一个按钮,调用第三方API,按钮十分钟之内只能点击一次,刷新页面也只能点击一次 2.思路 加一个本地缓存的时间戳,通过时间戳计算指定时间内不能点击按钮 3.实现 1)vue页面 & ...

  2. Visual Studio 卸载

    1.找个安装镜像文件 2.必须以管理员身份运行cmd 3.在CMD里输入"G:\vs_professional.exe /uninstall /force" 4.企业版就把prof ...

  3. new vue 实例发生了什么呢?

    前言 最近全面栽进vue源码解析中,将出一系列的学习笔记 以及个人的一些总结 第一步准备工作 到GitHub上下载vue的源码(巧妇难为无米之炊) 用自己喜欢的编辑器打开源码 vue主要源码資源在sr ...

  4. manim边学边做--场景Scene简介

    在 Manim 社区版本中,Scene(场景)是构建动画的核心概念之一,它为我们提供了一个结构化的方式来组织和呈现动画内容. 本文将介绍什么是Scene,它在Manim动画中的作用,以及不同类型的Sc ...

  5. DeFi(去中心化金融)的硬核知识

    1. ​DeFi流动性挖矿:躺着赚利息的"矿工"​ 简单来说,流动性挖矿就像你往银行存钱赚利息,但这里存的是加密货币,利息更高,还能随时提现.比如你往Uniswap这样的去中心化交 ...

  6. C# Semaphore

    1.Semaphore定义Semaphore,是负责协调各个线程, 以保证它们能够正确.合理的使用公共资源.也是操作系统中用于控制进程同步互斥的量. Semaphore常用的方法有两个WaitOne( ...

  7. Windows下安装使用OpenLDAP

    LDAP:(轻量级目录访问协议,Lightweight Directory Access Protocol)它是基于 X.500标准的,但是简单多了并且可以根据需要定制.与X.500不同,LDAP支持 ...

  8. 搭建docker swarm集群实现负载均衡

    Swarm简介:Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源.Swarm ...

  9. 比较 HashSet、LinkedHashSet 和 TreeSet 三者的异同

    比较 HashSet.LinkedHashSet 和 TreeSet 三者的异同HashSet.LinkedHashSet 和 TreeSet 都是 Set 接口的实现类,都能保证元素唯一,并且都不是 ...

  10. 学习unigui【20】unistringGrid

    做成下面效果图: 采用unistringGrid控件. 问题: 1.不同的日期区间如何得到.如: 项目   开始时间时间 -- 终止使用时间 呼吸机  yyyy-mm-dd   yyyy-mm-dd ...