非常经典的题目,求字符串中的最长回文子串。

(1)最朴素的解法 ---暴力 复杂度O(N³)

这也是最easy想到的方法。最外层循环枚举起点i,第二层循环从i+1開始向后枚举,第三层推断是不是回文串。最后取最长子串的返回。

代码比較简单,这里没有列出。

(2)中心扩展法。复杂度O(N²)

枚举每个字符作为中心点向左右扩展。

可是这里要注意,对于每一次扩展要分奇偶两种情况。

否则可能会漏掉情况。

一发现不满足的情况立即break进行下一个中心字符的推断,代码例如以下:

class Solution {
public:
string longestPalindrome(string s) {
string ans;
int len=s.length();
int maxLength=-1,CurLen,Sbegin;
for(int i=0;i<len;i++)
{
int left=i-1,right=i+1;
while(left>=0&&right<len&&s[left]==s[right])//奇数情况
{
CurLen=right-left+1;
if(CurLen>maxLength)
{
maxLength=CurLen;
Sbegin=left;
}
left--,right++;
} left=i,right=i+1;
while(left>=0&&right<len&&s[left]==s[right])//偶数情况
{
CurLen=right-left+1;
if(CurLen>maxLength)
{
maxLength=CurLen;
Sbegin=left;
}
left--,right++;
} }
ans=s.substr(Sbegin,maxLength);
return ans; }
};

(3)Manacher算法。复杂度仅仅有O(n)

详细算法介绍:点击打开链接

附上静止的代码:

class Solution {
public:
string preProcess(string s) {
int n = s.length();
if (n == 0) return "^$";
string ret = "^";
for (int i = 0; i < n; i++)
ret += "#" + s.substr(i, 1);
ret += "#$";
return ret;
} string longestPalindrome(string s) {
string T = preProcess(s);
int n = T.length();
int *P = new int[n];
int C = 0, R = 0;
for (int i = 1; i < n-1; i++) {
int i_mirror = 2*C-i; // equals to i' = C - (i-C) P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0; // Attempt to expand palindrome centered at i
while (T[i + 1 + P[i]] == T[i - 1 - P[i]])
P[i]++; // If palindrome centered at i expand past R,
// adjust center based on expanded palindrome.
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
// Find the maximum element in P.
int maxLen = 0;
int centerIndex = 0;
for (int i = 1; i < n-1; i++) {
if (P[i] > maxLen) {
maxLen = P[i];
centerIndex = i;
}
}
delete[] P; return s.substr((centerIndex - 1 - maxLen)/2, maxLen);
}
};

(4)后缀树的解法,待补充

[leetcode] Longest Palindromic Substring 多种解法的更多相关文章

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

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

  2. C++ leetcode Longest Palindromic Substring

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

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

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

  4. Leetcode Longest Palindromic Substring

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

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

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

  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题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

  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. hadoop safemode error

    http://www.cnblogs.com/hustcat/archive/2010/06/30/1768506.html 1.safemode bin/hadoop fs -put ./input ...

  2. jquery ajax 跨域

    客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算 ...

  3. MVC第一次访问比较慢的解决方案

    一.NGen优化 %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\ngen install EntityFramework.Core.dll %WINDIR ...

  4. spring mvc上传、下载的实现

    下载 //下载 @RequestMapping(value="/download") public ResponseEntity<byte[]> download() ...

  5. 触发器学习笔记(:new,:old用法)

    触发器学习笔记(:new,:old用法) 触发器是数据库发生某个操作时自动运行的一类的程序         用于保持数据的完整性或记录数据库操作信息方面         触发器不能够被直接调用,只能够 ...

  6. 使用PhoneGap开发基于Html5应用二:第一个PhoneGap应用:百度

    上一篇博文使用PhoneGap开发基于Html5应用一:PhoneGap简单介绍 中我介绍了怎样从phonegap官网上下载源代码并启动第一个应用,今天我们把phonegap的应用略微改一下,让他实现 ...

  7. HDUOJ---1712 ACboy needs your help

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. Web前端开发笔试&面试_01(mi:)

    —— (al_me16041719002000) begin—— 1.(单选)下面哪个方法是String对象和Array对象都有的? A.splice B.split C.replace D.conc ...

  9. Linux命令-终止进程命令:pkill

    killall是杀死所有进程,而pkill是按照进程名称杀死进程,可以达到杀死所有进程的目的,因为linux里面同名的进程是分主进程和子进程的. pkill - httpd 按名称强制杀死httpd进 ...

  10. 理解over()函数

    1.1.两个order by的执行时机分析函数(以及与其配合的开窗函数over())是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的ord ...