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

(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. UITableViewCell 取消选中的蓝色背景

    方案一: [self.tableView setAllowsSelection:NO]; 方案二: [cell setSelectionStyle:UITableViewCellSelectionSt ...

  2. JSP的页面连接和提交方式(web基础学习笔记六)

    一.GET请求新页面 1.1.超链接请求新页面 <!-- 超链接到page2 --> <a href="page2.jsp">链接到page2</a& ...

  3. Oracle体系结构一(学习笔记)

    总体结构分为三个部分:SGA,PGA,FILE文件 按功能分: 存储结构  存储结构对应关系  主要文件: 数据文件: 每个数据文件只与一个数据库相关联 一个表空间可以包含一个或者多个数据文件 一个数 ...

  4. __inline定义的内联函数和宏的区别

    转自:http://blog.csdn.net/lw370481/article/details/7311668 函数与宏 #define TABLE_COMP(x) ((x)>0?(x):0) ...

  5. VIM经常使用操作

    VIM使用 移动命令 按键 说明 h 左 l 右(小写L) j 下 k 上 w 移动到下一个单词 b 移动到上一个单词 进入插入模式 命令 说明 i 在当前光标处进行编辑 I 在行首插入 A 在行末插 ...

  6. html5调用摄像头实现拍照

    技术时刻都在前进着.我们的需求也是时刻在改变着.最近在开发中遇到了用户进行账号注册时需要个人图像,网站提供自动拍照功能.还有在登录了PC之后,手机端进行登录时只需要扫描一下PC上的二维码就可以登录.这 ...

  7. 项目中用到的ext及js细节

    1.js中无replaceAll方法,但能够用replace(regex," "),第一个參数是正則表達式,第二个參数是string,eg:str.replace(/\r\n/g, ...

  8. 24、java操作xml方法

    XML解析方式 1. SAX解析方式 SAX(simple API for XML)是一种XML解析的替代方法.相比于DOM,SAX是一种速度更快,更有效的方法.它逐行扫描文档,一边扫描一边解析.而且 ...

  9. Log4net的配置-按照日期+文件大小混合分割

    ender name="DebugAppender" type="log4net.Appender.RollingFileAppender"><fi ...

  10. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...