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 ...
随机推荐
- Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云!
Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云! Chef.Puppet.Ansible.SaltStack 都可以称为配置管理工具,这些工具的主要目 ...
- Ubuntu16.04安装视觉SLAM环境(g2o)
1.首先在github上下载g2o图优化库 git clone https://github.com/RainerKuemmerle/g2o.git 2.运行安装以下依赖库 sudo apt-get ...
- switch case执行顺序
public class SwitchCase { public static void main(String[] args) { System.out.println(switchFun(4)); ...
- maven 打包 war 包含 WEB-INF/lib 目录
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- Android字符串及字符串资源的格式化
为什么要写这一篇随笔呢?最近做项目的过程中,遇到很多页面在要显示文本时,有一部分是固定的文本,有一部分是动态获取的,并且格式各式各样.一开始采取比较笨的办法,把他拆分成一个个文本控件,然后对不同的控件 ...
- Web安全之CSRF攻击的防御措施
Web安全之CSRF攻击的防御措施 CSRF是什么? Cross Site Request Forgery,中文是:跨站点请求伪造. CSRF攻击者在用户已经登录目标网站之后,诱使用户访问一个攻击 ...
- (转)Mysql主从复制搭建及详解
http://www.cnblogs.com/kevingrace/p/6256603.html---------Mysql主从同步(1)-主从/主主环境部署梳理 原文:http://blog.csd ...
- 深度学习(十三) R-CNN Fast RCNN
object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.object detection要解决的问题就是物体在哪里,是什么这整个流程的问题.然而,这个问题 ...
- WPF DataTemplate與ControlTemplate
一. 前言 什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...