【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目:
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
思路:用Manacher算法得到最大回文子串的长度,得到带#的最大回文子串,用split剔除#后转化成String形式输出即可。
public class Solution {
public String longestPalindrome(String s) {
if(s==null || s.length()==0){
return null;
}
char[] charArr=manacherString(s);
int[] pArr=new int[charArr.length]; int index=-1;
int center=-1;
int pR=-1;
int max=0;
for(int i=0;i<charArr.length;i++){
pArr[i]=pR>i?Math.min(pArr[2*index-i],pR-i):1; while(i+pArr[i]<charArr.length && i-pArr[i]>-1){
if(charArr[i+pArr[i]]==charArr[i-pArr[i]]){
pArr[i]++;
}else{
break;
}
}
if(i+pArr[i]>pR){
pR=i+pArr[i];
index=i;
} if(pArr[i]-1>max){
max=pArr[i]-1;//得到最大回文子串的长度
center=i;
}
}
//以string形式返回最大回文子串
String str=new String(charArr);
String[] strArr=str.substring(center-max,center+max).split("#");
StringBuffer sb=new StringBuffer();
for(int i=0;i<strArr.length;i++){
sb.append(strArr[i]);
}
return sb.toString();
} public static char[] manacherString(String str){
char[] charArr=str.toCharArray();
char[] res=new char[2*str.length()+1];
int index=0;
for(int i=0;i!=res.length;i++){
res[i]=(i&1)==0?'#':charArr[index++];
}
return res;
}
}
【LeetCode】5. Longest Palindromic Substring 最大回文子串的更多相关文章
- 5. Longest Palindromic Substring最大回文子串
int sta = 0; int max = 1; public String longestPalindrome(String s) { /* 判断回文有两种: 1.最大回文子序列求长度: 用动态规 ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Longest Palindromic Substring-----最长回文子串
首先讲讲什么是回文, 看看Wiki是怎么说的:回文,亦称回环,是正读反读都能读通的句子.亦有将文字排列成圆圈者,是一种修辞方式和文字游戏.回环运用得当.能够表现两种事物或现象相互依靠或排斥的关系, 比 ...
- [leetcode]516. Longest Palindromic Subsequence最大回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
随机推荐
- 【KVM】Ubuntu14.04 安装KVM
1. 首先检查系统是否支持CPU虚拟化 # egrep -o "svm|vmx" /proc/cpuinfo 若显示如下类似信息,则说明支持CPU虚拟化 vmx vmx ... v ...
- FFTW简介及使用
http://fftw.org/ FFTW is a C subroutine library for computing the discrete Fourier transform (DFT) i ...
- python学习-day11-内建函数
python-内建函数 -int:将字符串转换为数字 a = " print(type(a),a) b = int(a) print(type(b),b) num = " v = ...
- HDMI EDID解读
现在的显示设备比如显示器,电视等都HDMI接口,那通常每个HDMI接口都保留有一份EDID数据,这个数据可以存在程序里面由系统启动过程中来初始化,更常见的做法是每个HDMI口会有一个EEPROM来保存 ...
- TMS320C54x系列DSP的CPU与外设——第2章 TMS320C54x DSP体系结构总体介绍
第2章 TMS320C54x DSP体系结构总体介绍 本章介绍TMS320C54x DSP体系结构的概况,包括中央处理单元(CPU).存在器和片内外设. C54x DSP采用了高级的改进哈佛结构,用8 ...
- C#中的 int?是什么意思
http://www.cnblogs.com/firstcsharp/archive/2011/12/11/2283797.html int?:表示可空类型,就是一种特殊的值类型,它的值可以为null ...
- JavaScript权威指南 第七章 数组
主要介绍一下数组方法 1.Join() Array.join()方法将数组中所有元素都转换为字符串并连接在一起,返回最后生成的字符串. 可以指定一个可选的字符串在生成的字符串中来分隔数组的各个元素.默 ...
- 黄聪:WordPress固定链接设置的几种方法
wordpress固定链接设置的一些参数: %year%:基于文章发布的年份,比如2010: %monthnum%:基于文章发布的月份,比如01: %day%:基于文章发布当日,比如06: %hour ...
- svn在linux下的使用(转)
ubuntu命令行模式操作svn 首先要安装SVN客户端到你的系统才能操作各种命令 apt-get install subversion 1.将文件checkout到本地目录 svn checkout ...
- (C/C++) Interview in English - Class
Q: What is a class? A: A class is an expanded concept of a data structure: instead of holding only d ...