Question: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.(给定一个字符串S,在S中找到最长的回文子字符串,假定最长的回文字符串长度是1000,并且在这个字符串中存在唯一的一个最长回文子字符串)

  今天做到leetcode上的这个题,没有想到这个题也竟然是百度14年的笔试题,题目大体相同。下面我来分享一下我在做这个题的时候的一些感悟。

  最开始,对这个题没有思路,想到一种很笨的方法,也就是穷举的思想,能够得到最长的回文子字符串,但是leetcode测试时间超时,下面是这种方法的代码(Java)

 class Solution {
//超过leetcode时间要求
public String longestPalindrome(String s) {
// Note: The Solution object is instantiated only once and is reused by each test case
String str=new String();
int max=0;
//穷举法是很笨的方法
for(int i=0;i<s.length();i++){
for(int j=i+2;j<=s.length();j++){
if(isPalindrome(s.substring(i,j))&&(j-i)>max){
str=s.substring(i,j);
max=j-i;
}
}
}
return str;
}
//判断是否是回文字符串
public Boolean isPalindrome(String s){
for(int i=0;i<s.length()/2;i++){
if(s.charAt(i)!=s.charAt(s.length()-i-1))
return false;
}
return true;
}
}

第二种方法思路是:首先把字符串s反转得到str1,s与str1相同的字符串就有可能是回文子字符串,之所以说有可能是因为有一种情况求出的相同字符串不是所要求的,比如s="12343943215",下面我会做详细介绍,第二种方法代码如下:

 class Solution5{
public String longestPalindrome(String s) {
String str=reverseNewString(s);
return longestSubString(s, str);
}
/**
* 在不改变原有字符串排列的情况下反序
* */
public String reverseNewString(String s){
String str="";
for (int i=s.length()-1;i>=0;i--) {
str+=s.charAt(i);
}
return str;
}
/**
* str1和str2是两个反序的字符串,他们两者中相同的最长的子字符串就是最长的回文字符串
* */
public String longestSubString(String str1,String str2){
int length=str1.length();//字符串长度
int [] array=new int[length+1]; //以空间换时间
int max=0;//字符串最大长度
int max_j=0;//下标记录
for(int i=0;i<length;i++){
for(int j=length-1;j>=0;j--){
if(str1.charAt(i)==str2.charAt(j)){
array[j+1]=array[j]+1;
}
else{
array[j+1]=0;
}
// if(array[j+1]>=4){
// System.out.println("大于4 "+" "+array[j+1]+" "+j);
// System.out.println(str2.indexOf("yvvy")+"==="+str2.substring(182,186)+" "+str1.substring(760, 764));
// System.out.println(str2.indexOf("cltg")+"==="+str2.substring(209, 213)+" "+str1.substring(733,737));
// System.out.println(str2.substring(253,257)+" "+str1.substring(689, 693));
// }
if(array[j+1]>max&&str2.substring(j+1-array[j+1],j+1).equals(str1.substring(length-j-1,length-j-1+array[j+1]))){
max=array[j+1]; //记录字符串最大长度
max_j=j+1; //记录字符串最后字母位置
}
}
}
// System.out.println("array[695]="+str1.substring(692,696));
if(max_j>0)
return str2.substring(max_j-max,max_j); //根据字符串长度max,和字符串最后的字母的位置max_j截取字符串
else
return null; //如果记录的下标为0,,返回空
}
}

如果在代码中没有这句①str2.substring(j+1-array[j+1],j+1).equals(str1.substring(length-j-1,length-j-1+array[j+1])),那么leetcode 测试86个案例会通过85个,那一个通不过的是哪种情况?

就是属于上边的s="12343943215"的情况,s的反转字符串str1=“51234934321”,没有上边的判断两个所要截取字符串相等的语句①,黄色数字部分比较结果会认为是相同的字符串,会输出最长回文子字符串为“1234“,”1234“肯定不是,最长应该为的为”343“。

leetcode测试没有判断语句①的情况:

leetcode测试有判断语句①的情况:

以上的第二种算法时间复杂度O(n^2),还有更好的算法,欢迎大神吐槽,多多交流。

另外本文参照了云淡风轻kevin Lee的博客http://www.cnblogs.com/kevinLee-xjtu/archive/2011/12/21/2299082.html,并弥补了一个bug.

leetcode:Longest Palindromic Substring(求最大的回文字符串)的更多相关文章

  1. 5. Longest Palindromic Substring[M]最长回文子串

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

  2. 21.Longest Palindromic Substring(最长回文子串)

    Level:   Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...

  3. 面试常用算法——Longest Palindromic Substring(最长回文子串)

    第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...

  4. LeetCode:Longest Palindromic Substring 最长回文子串

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

  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] Longest Palindromic Substring(manacher algorithm)

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

  7. Leetcode Longest Palindromic Substring

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

  8. C++ leetcode Longest Palindromic Substring

    明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...

  9. Leetcode: Longest Palindromic Substring && Summary: Palindrome

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

随机推荐

  1. 【USACO】

    Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to plea ...

  2. mvp(2)一个简单示例,加深理解

    参考: http://www.cnblogs.com/liuling/p/mvp-pattern-android.html 架构图: 1.View层 public interface NewsView ...

  3. MVC列表页通过CheckBox进行批量选择删除

    1.Html代码,将所有CheckBox包含于删除表单,并且所有列表项的CheckBox使用相同的Name,并且Value设置为数据项主键ID @using (Html.BeginForm(" ...

  4. DbProviderFactories.GetFactoryClasses

    using System.Data.Common; private void Method1() { DataTable table = DbProviderFactories.GetFactoryC ...

  5. Linux常用到的指令汇总

    Linux常用到的指令汇总 根据鸟哥linux私房菜上定义的:一定要先學會的指令:ls, more, cd, pwd, rpm, ifconfig, find 登入與登出(開機與關機):telnet, ...

  6. 【转载】正则表达式学习 & ASCII码表

    文章原地址: http://www.jb51.net/tools/zhengze.html <正则表达式30分钟入门教程> 其中有几个地方可以有笔记: \s 匹配任意的空白符 \b 匹配单 ...

  7. Codeforces 475 B Strongly Connected City【DFS】

    题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...

  8. 代码记录:使用Aforge.net让视频图像反转180度

    private void CameraConn() { videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedI ...

  9. POJ 2594 Treasure Exploration (可相交最小路径覆盖)

    题意 给你张无环有向图,问至少多少条路径能够覆盖该图的所有顶点--并且,这些路径可以有交叉. 思路 不是裸的最小路径覆盖,正常的最小路径覆盖中两个人走的路径不能有重复的点,而本题可以重复. 当然我们仍 ...

  10. ecshop init.php文件分析(转)

    <?php /** * ECSHOP 前台公用文件 */ //防止非法调用 defined-判断常量是否已定义,如果没返回false if (!defined('IN_ECS')) { die( ...