DP、KMP什么的都太高大上了。自己想了个朴素的遍历方法。

【题目】

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.

【思路】(应该算是O(n)吧)

从中间向两端搜索。分别找到以每一个字母为中心的最长回文子串,假设两边剩余的元素已经比当前最长回文子串的一半还短时,停止遍历。

大家别看代码长。是为了便于理解的。

【Java代码】

public class Solution {
public String longestPalindrome(String s) {
int len = s.length();
if (s == null || len == 1){
return s;
} String ret = "";
int mid = len / 2;
int i = 0;
while (true) {
//遍历到s两端时。假设以mid+i或mid-i为中心的最长回文都不比当前最优解长,遍历结束
if (2*(len-(mid+i)) < ret.length() && 2*(mid-i+1) < ret.length()) {
break;
}
String str1 = palindrome1(s, mid+i);
String str2 = palindrome2(s, mid+i);
String str3 = palindrome1(s, mid-i);
String str4 = palindrome2(s, mid-i);
if (str1.length() > ret.length()) {
ret = str1;
}
if (str2.length() > ret.length()) {
ret = str2;
}
if (str3.length() > ret.length()) {
ret = str3;
}
if (str4.length() > ret.length()) {
ret = str4;
}
i++;
} return ret;
} //找出s中以index为中心的aba型的回文子串
public String palindrome1(String s, int index) {
String ret = "";
int i = index, j = index;
while (i>=0 && j<s.length()) {
if (s.charAt(i) != s.charAt(j)) {
break;
}
ret = s.substring(i, j+1);
i--;
j++;
}
return ret;
} //找出s中以index和index+1为中心的abba型回文子串
public String palindrome2(String s, int index) {
String ret = "";
int i = index, j = index+1;
while (i>=0 && j<s.length()) {
if (s.charAt(i) != s.charAt(j)) {
break;
}
ret = s.substring(i, j+1);
i--;
j++;
}
return ret;
}
}

【分析】

后来在网上找了类似的解法。

如 http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html ,为了不用区分aba型和abba型的回文子串,构造了一个新的字符串 t ,在两端和每两个字母之间插入一个特殊字符 ‘#’ 。这样当以‘#’为中心的回文子串就是我的代码中abba型子串。

我的代码与之相比还使用了一个小trick,即从中间向两端遍历。这样假设中间已经有比較长的回文子串了,那么两端比較偏的回文子序列就可省去推断。

一遍就AC了。后来比較了网上的解法,不禁为自己有点小激动呢~。

分析可能有误,或许仅仅是自我感觉良好,欢迎大家指正!

【LeetCode】Longest Palindromic Substring 解题报告的更多相关文章

  1. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  2. [LeetCode] Longest Palindromic Substring 最长回文串

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

  3. Leetcode Longest Palindromic Substring

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

  4. [LeetCode] Longest Palindromic Substring(manacher algorithm)

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

  5. C++ leetcode Longest Palindromic Substring

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

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

    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 ...

  8. Leetcode: Longest Palindromic Substring. java

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

  9. LeetCode——Longest Palindromic Substring

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

随机推荐

  1. Android开发新手HelloWorld解析

    首先看这个 HelloWorld 类. Java代码public class HelloWorld extends Activity {       /** Called when the activ ...

  2. axios统一拦截配置

    在vue项目中,和后台进行数据交互使用axios.要想统一处理所有的http请求和响应,就需要使用axios的拦截器.通过配置http response inteceptor 统一拦截后台的接口数据, ...

  3. django-10-中间件和上下文管理器

    <<<中间件的引入>>> 用户<->中间件<->url->视图  在app目录里面 middleware.py  (1)中间件就是一个 ...

  4. linux环境下删除包含特殊字符的文件或目录

    linux环境下删除包含特殊字符的文件或目录 ls -liUse find command as follows to delete the file if the file has inode nu ...

  5. HashMap导致死循环问题

    虽然我推测是链表形成闭环,但 没有去证明过.从网上找了一下: http://blog.csdn.net/autoinspired/archive/2008/07/16/2662290.aspx 里面也 ...

  6. 彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的

    彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的 1.线程池,thread = threadPool.getThread(),thread.executeHtt ...

  7. java之 ------ DAO设计模式的【具体解释】及常见设计模式的【应用】

    DAO Data Access Object(数据訪问接口) 一.场景和问题 在Java程序中.常常须要把数据持久化,也须要获取持久化的数据.可是在进行数据持久化的过程中面临诸多问题(如:数据源 不同 ...

  8. cocos2d-x ios游戏开发初认识(九) 音效、粒子系统与存储

    我们知道.一个游戏少不了声音.一些好听的声音会提起你对游戏的兴趣,当然做好听的声音不是我们要学的,我们的目的是把声音在适当的时候放出来.顺便在这节中会说下简单的粒子系统和文件存储. 一.声音的播放: ...

  9. Oracle SGA具体解释

    SGA(SYSTEM Global Area )系统全局区 l 数据快速缓存 在Oracle进行数据处理的过程中,代价最昂贵的就是物理 I/O操作了.相同的数据从内存中得到要比从磁盘上读取快的多. 因 ...

  10. windows+ubuntu双系统,在windows中访问ubuntu文件

    今天被告知ubuntu磁盘空间不足,百度得知可以通过autoremove命令清理,然而,,再也进不去ubuntu系统了,具体表现为第一次选择ubuntu之后一直是空白紫屏,如果强制关机再开机后选择ub ...