[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 ...
随机推荐
- Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xml, reason: Connection to https://dl-ssl.google.com refused
修改hosts文件.文件在C:\WINDOWS\system32\drivers\etc目录下,Linux用户打开/etc/hosts文件.用记事本打开文件后添加以下内容. #Google主页203. ...
- LoadRunner录制:集合点
背景 LoadRunner 执行过程中,有的user 跑的快,有的跑的慢.就导致user1可能还在执行 登录操作呢,user2都已经开始执行查询操作了. 但是在进行负载测试时 ,我们又需要让很多用户同 ...
- oracle 建表时显示ORA-00984: 列在此处不允许
oracle 建表时显示ORA-00984: 列在此处不允许 CreationTime--2018年7月19日16点10分 Author:Marydon 1.情景展示 使用plsql建表时,报错 ...
- 〖Linux〗build sqlite3 for Arm
Version: sqlite-autoconf-3080100.tar.gz Download: https://www.sqlite.org/download.html 1. toolchains ...
- HDU 1017 A Mathematical Curiosity (数学)
A Mathematical Curiosity Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56150 Accepted: 19398 Desc ...
- C-I/O操作函数详解
EOF: End Of File, 文字流结尾, 这里的文字流可以是文件(file), 也可以是标准输入(stdin), 它的值在任何可能出现的字符之外(-1) 先列出三种基本类型操作函数 这里面返回 ...
- [转载]meclipse中project facet问题
原文地址:meclipse中project facet问题作者:丫头_樱桃 一般出现在从别处import的项目上,只有项目文件夹上有红叉,其他地方都正常,现总结个人的几个解决方案: 有几种可能: 1, ...
- 浅析iOS tableview的selectRowAtIndexPath选中无效(默认选中cell无效)
可能很多人都遇到过这种情况: tableview列表,有时加载完,需要默认选中某一行,给予选中效果:或者需要执行某行的点击事件. 我们举例: 比如我想默认选中第一行 可能我们第一个想法就是这样: [m ...
- matplot模块中的pylab
pylab的目的 Pylab combines the functionality of pyplot with the capabilities of NumPy in a single names ...