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. JS中Math函数基础

    Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’

  2. HDU 4332 Contest 4

    顶好的一道题.其实,是POJ 2411的升级版.但POJ 2411我用的插头DP来做,一时没想到那道题怎么用状态DP,于是回头看POJ 2411那一道的状态DP,其实也很简单,就是每一行都设一个状态, ...

  3. iOS开发自己定义键盘回车键Return Key

    在iOS开发中.用户在进行文本输入的时候,往往会用到虚拟键盘上的回车键,也就是Return Key.回车键有时候能够是"完毕"(表示输入结束).能够是"下一项" ...

  4. Picking up Jewels

    Picking up Jewels There is a maze that has one entrance and one exit.  Jewels are placed in passages ...

  5. Android This Activity already has an action bar supplied by the window decor

    This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ ...

  6. UVA 1016 - Silly Sort 置换分解 贪心

                                           Silly Sort Your younger brother has an assignment and needs s ...

  7. javax.validation参数校验

    在实体字段加注解: /** * 机构名称 */ @ApiParam(name = "orgName", value = "机构名称") @Size(max = ...

  8. JS基本功 | JavaScript专题之数组 - 方法总结

    Array.map() 1.   map() 遍历数组 语法: let new_array = arr.map(function callback(currentValue, index, array ...

  9. dl learn task

    https://deeplearning4j.org/cn/word2vec Task 1 分类http://blog.csdn.net/czs1130/article/details/7071734 ...

  10. hiho 1590 - 紧张的会议室。区间问题

    题目链接 小Hi的公司最近员工增长迅速,同时大大小小的会议也越来越多:导致公司内的M间会议室非常紧张. 现在小Hi知道公司目前有N个会议,其中第i个会议的时间区间是(Si, Ei). 注意这里时间区间 ...