[leetcode] Longest Palindromic Substring 多种解法
非常经典的题目,求字符串中的最长回文子串。
(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 多种解法的更多相关文章
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode——Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- wine的使用
ubuntu-ubuntu10.04使用wine安装SourceInsight http://www.cnblogs.com/eddy-he/archive/2012/03/08/ubuntu_win ...
- SQL:1999基本语法(学习笔记)
SQL:1999基本语法 SELECT [DISTINCT] * | 列名称 [AS]别名,........ FROM 表名称1 [别名1][CROSS JOIN表名称2 别名2]| [NATURAL ...
- Android逆向之旅---带你爆破一款应用的签名验证问题
一.前言 在之前的文章中说过Android中的安全和破解是相辅相成的,为了防止被破解.非常多应用做了一些防护策略.可是防护策略也是分等级.一般简单的策略就是混淆代码和签名校验.而对于签名校验非常多应用 ...
- Java多线程系列目录(转)
转载方便自己学习,转自:Java多线程系列目录(共43篇) http://www.cnblogs.com/skywang12345/p/java_threads_category.html 最近,在研 ...
- Makefile 中:= ?= += =的区别【转】
转自:http://www.cnblogs.com/wanqieddy/archive/2011/09/21/2184257.html 在Makefile中我们经常看到 = := ?= +=这几个赋值 ...
- 去掉cb中括号的匹配
Settings->Editor->General settings->Indent options->Brace completion``
- HDUOJ --2566
统计硬币 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDUOJ---(2203)亲和串
亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- ios NSURLSession使用说明及后台工作流程分析
NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...
- C# 打开钱箱支持北洋、佳博、爱普生
/// <summary> /// 执行开钱箱操作 /// 没钱箱或打印机原功能都可以正常使用 /// </summary> public void ExecuteOpenCa ...