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

(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. C++运行库 Neptune C++ Runtime Library(xbmc)

    一个可以在多个平台C++开发环境下编译运行的C++运行库.其中包括了对多个C++库和平台SDK(HTTP/TCP/UDP/XML, Thread/Message, String, List/Stack ...

  2. [转]查看linux服务器硬盘IO读写负载

    最近一台linux服务器出现异常,系统反映很慢,相应的应用程序也无法反映,而且还出现死机的情况,经过几天的观察了解,发现服务器压力很大,主要的压力来自硬盘的IO访问已经达到100% 为了方便各位和自己 ...

  3. 谷歌地图api訪问失败

    在非外网情况下.我们调用谷歌api会出现载入不到地图的现象.此时能够换一下域名试试或许就好了 比方我自己訪问api时时这样写的: https://maps.googleapis.com/maps/ap ...

  4. java 图片数据Base64编解码

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  5. 〖Linux〗apt-get wait for another apt process

    #!/bin/bash i= tput sc >& || \ >&; do )) in ) j="-" ;; ) j="\\" ;; ...

  6. Struts2的配置文件的配置struts.xml

    在学习struts的时候,我们一定要掌握struts2的工作原理. 仅仅有当我们明白了在struts2框架的内部架构的实现过程.在配置整个struts 的框架时.能够非常好的进行逻辑上的配置.接下来我 ...

  7. oper

    package main.java.com.zte.controller.ems; import java.util.HashMap; import java.util.List; import ja ...

  8. Apache Hadoop 3.0新版本介绍及未来发展方向

    过去十年,Apache Hadoop从无到有,从理论概念演变到如今支撑起若干全球最大的生产集群.接下来的十年,Hadoop将继续壮大,并发展支撑新一轮的更大规模.高效和稳定的集群. 我们此次将向大家全 ...

  9. kubectl命令使用

    语法:   kubectl  [command]  [TYPE] [NAME]  [flags]   1 command:子命令,用于操作Kubernetes集群资源对象的命令,如create, de ...

  10. java测试Unicode编码以及数组的运用(初学篇)

    /*第二章第四小题*/ /* * (1)编写一个应用程序,给出汉字“你” ,“我”,“他”在Unicode 表中的位置 * (2)编写一个java应用程序,输出全部的希腊字母 */ public cl ...