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 ...
随机推荐
- 6、TensorFlow基础(四)队列和线程
队列和线程 和 TensorFlow 中的其他组件一样,队列(queue)本身也是图中的一个节点,是一种有状态的节点,其他节点,如入队节点(enqueue)和出队节点(dequeue),可以修改它的内 ...
- python中TAB补全
tab补全的代码文件tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcom ...
- 【算法笔记】A1063 Set Similarity
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- ActionBarCompat 教程-实现Action Bar
http://www.mobiletuts.me 自Action Bar设计概念在Android 3.0(API 11) 中被Google引入以后,Action Bar这种设计模式迅速被各APP厂商( ...
- Java中的语法树结构
1.JCTypeParameter class B<T extends CA&IA&IB> { ...} 截图如下: 接口继承了StatementTree接口,而实现类实现 ...
- wpf 自定义ListView
1.listview.itemtemplate设置item的外层父元素的控件. 2.listview.template设置item的样式(datatemplate),也可以使用itemcontaine ...
- [C语言]日期间天数差值的计算
刷一些算法题时总能遇到计算日期间天数的问题,每每遇到这种情况,不是打开excel就是用系统自带的计算器.私以为这种问题及其简单以至于不需要自己动脑子,只要会调用工具就好.直到近些天在写一个日历程序的时 ...
- 05 JDK1.5 Lock锁
一.synchronized的再次讨论 使用synchronized关键字来标记一个方法或者代码块,当某个线程调用该对象的synchronized方法或者访问synchronized代码块时, 这个线 ...
- redis的数据类型(一) key操作
redis是一个key-value形式的数据缓存,因此包括key和value. 一.key的说明 1.redis的key Redis的key是字符串类型,但是key中不能包括边界字符,由于ke ...
- Python(3):文件读写与异常
访问路径: 文件读写必然涉及到文件会放在某个路径下.在python里,可以通过引入os包来实现切换当前访问的路径: # 假设我在 /home/zyq/KiDe/Python/test 文件夹中有一个文 ...