LeetCode算法题5----Longest Palindromic Substring
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.
算法分析
方法1
从最左边的字符(指标为 i )开始,从末尾寻找同该字符相同的字符(指标为 j ),然后依次比较 i++ 和 j-- 处的字符,如果相同,++i 和 --j,继续进行比较。直到 j<=i,说明当前考察的子字符串为 Palindromic;否则不是 Palindromic。
该方法的弊端就是太容易做无用功了:因为很可能出现两端字符依次相等而只有中间一小段的字符不相等的情况了。比如 "abcdefghigfedcba",只有比较到中间才会发现 h 和 i 不相等。
方法2
基于上述的问题,我们比较字符串的时候,从当前考察的子字符串的中间开始比较,因为 Palindromic 的字符串一定是镜像的,也就是说从中间向两端依次相等。我们可以从 i=0 到 i=s.length()-1 来遍历原字符串 s ,只要得出以 s.charAt(i) 为中间字符的字符串和以 s.charAt(i)与 s.charAt(i+1) 为中间两个字符的字符串的镜像长度,去二者最大者,就可以得出当前考察的 i 的镜像长度。
方法3
方法2是从 i=0 到 i=s.length()-1 来考察的,这回导致一些没必要的考察,因为很可能比较长的镜像字符串在中间位置,而较短的镜像字符串在边缘位置,这样,边缘的较短的镜像字符串根本就没必要去考察,所以,我们应该从字符串 s 的中间开始考察镜像字符串,如果出现了中间某个镜像字符串的的长度已经到达了字符串 s 的末端(即 s.charAt(0)或 s.charAt(s.length()-1) ),那么就可以知道当前考察的子字符串就是最长的镜像字符串了。
Java 算法实现
public class Solution {
public String longestPalindrome(String s) {
if(s==null){
return null;
}
int start=0,end=0;
int length=s.length();
int mid1=(length-1)>>>1;
int mid2=length>>>1;
int len=1,lenTmp;
if(mid1!=mid2){ //此时原字符串的长度一定为偶数
lenTmp=getLen(s, mid1, mid2);
if(lenTmp>len){
len=lenTmp;
start=mid1-((len>>1)-1);//len一定是偶数
end=mid2+((len>>1)-1);
}
}
int len1,len2;
for(int i=mid1,j=mid2;i>0&&j<length-1;i--,j++){
if(len>=((i<<1)+1)){
//接下来的长度已经不可能再比len更长了,没有比较的必要了
break;
}
len1=getLen(s, i, i);
len2=getLen(s, i-1, i);
lenTmp=Math.max(len1, len2);
if(lenTmp>len){
len=lenTmp;
start=i-(len>>>1);//i-((len-1)>>>1);
end=i+((len-1)>>>1);
if(len>=(i<<1)){
//已经达到最大长度,下面的长度就没有比较的必要了
return s.substring(start,end+1);
}
}
len1=getLen(s, j, j);
len2=getLen(s, j, j+1);
lenTmp=Math.max(len1, len2);
if(lenTmp>len){
len=lenTmp;
start=j-((len-1)>>>1);
end=j+(len>>>1);
if(len>=((length-j)<<1)){
//已经达到最大长度,下面的长度就没有比较的必要了
return s.substring(start,end+1);
}
}
}
return s.substring(start,end+1);
}
public int getLen(String s,int mid1,int mid2){
int L=mid1,R=mid2;
while(L>=0&&R<s.length()&&(s.charAt(L)==s.charAt(R))){
L--;
R++;
}
return (R-L-1);
}
}
LeetCode算法题5----Longest Palindromic Substring的更多相关文章
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- 刷题5. Longest Palindromic Substring
一.题目说明 Longest Palindromic Substring,求字符串中的最长的回文. Difficuty是Medium 二.我的实现 经过前面4个题目,我对边界考虑越来越"完善 ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
随机推荐
- 使用GPIO监听中断
#include<stdlib.h> #include<stdio.h> #include<string.h> #include<unistd.h> # ...
- LARTC
大牛的博客 howto ,however, is simplify. another space ip link list ip address show ip route show route -n ...
- Python pip离线部署
因为生产环境不能联网,必须使用离线部署pip包,倒也不用部署Pypi镜像那么大工作量,其实蛮简单的,贴出了备忘 pip download -r requirements.txt -d packages ...
- Jenkins 插件升级时跳过 update site 的签名验证
当升级jenkins插件时,如果链接的update site用的自签名证书,可以用这个选项来启动Jenkins,来跳过签名验证: -Dhudson.model.DownloadService.noS ...
- notepad++中配置python运行命令
1.安装notepad++ 2.打开notepad++ 3.键盘上按下“F5”,在弹出的命令菜单中输入“cmd /k C:\Python30\python.exe "$(FULL_CURRE ...
- DB2 命令大全
check Archiving processing 查看日志归档情况 db2 "SELECT DATE(CAST(START_TIME as TIMESTAMP)) as DATE, co ...
- Jmeter创建一个 JMS 主题的测试计划
新建一个 JMS 主题的测试计划 JMS 需要下载一些可选的jar 文件.详细信息请参阅 第一章:新手入门.在本章节,将学习如何创建测试计划来测试JMS提供程序.创建5个订阅者和1个发布者.创建2个线 ...
- ruby gems列表
https://github.com/shageman/cobradeps
- 解决图片浮动调节不了的问题(使用vertical-align属性)
vertical-align: middle; vertical-align 属性设置元素的垂直对齐方式. baseline 默认.元素放置在父元素的基线上.sub 垂直对齐文本的下标.super ...
- 剑指offer(1-10)编程题
二维数组中的查找 替换空格 从尾到头打印链表 重建二叉树 用两个栈实现队列 旋转数组的最小数字 斐波那契数列 跳台阶 变态跳台阶 矩形覆盖 1 .在一个二维数组中,每一行都按照从左到右递增的顺序排序, ...