Leetcode0005--Longest Palindromic Substring 最长回文串
【转载请注明】http://www.cnblogs.com/igoslly/p/8726771.html
来看一下题目:
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Example: Input: "cbbd" Output: "bb" |
题目意思: 给出字符串中最长的回文串 若长度相同,给出位置最前的 |
作为较经典的题目,回文串通常有几种方法,已经有很多人分析过了,提供链接:https://segmentfault.com/a/1190000005063336
总的来说:
1、穷举法
对于长度为 n 的字符串,有字符串 n(n+1)/2 ,判断是否回文串复杂度为 O(n),算法整体复杂度为 O(n^3)
2、中心扩展法
对于回文串,从对称轴展开的字符均相同;把字符串的每个位置作为回文串的对称轴,判断回文串的最大长度;子串对称轴遍历复杂度为O(n),回文判断O(n)
这里要注意:长度为奇数 / 偶数时,对称轴的位置不同
class Solution {
public:
int max=;
string res="";
string longestPalindrome(string s) {
if(s.size()==){return s;}
int len=s.size();
for(int i=;i<len-;i++){
// 字符串从0 ~ len-2位置,i&i进行奇数判断,i&i+1进行偶数判断
check(s,i,i);
check(s,i,i+);
}
return res;
}
// 判断回文串最大长度
void check(string s,int i,int j){
while(i>=&&j<s.size()){
// 若两边扩展字符相等,更新最大长度
if(s[i]==s[j]){
if(j-i+>max){
max=j-i+;
res=s.substr(i,max);
}
i--;
j++;
}else{
return;
}
}
}
};
给出一个Leetcode大神的代码,也是以中心扩展法为基本思想
class Solution {
public:
string longestPalindrome(string s) {
// 去除长度为0、1情况
if (s.empty()) return "";
if (s.size() == ) return s;
// 记录最长回文串的起始位置、最大长度
int min_start = , max_len = ;
for (int i = ; i < s.size();) {
if (s.size() - i <= max_len / ) break;
int j = i, k = i; // 以i作为中心位置,进行两边扩展
// 若中心毗邻字符串,则直接包含在内;因为中心位置相同字母必然对称
while (k < s.size()- && s[k+] == s[k]) ++k; // Skip duplicate characters.
i = k+;
// 以j,k向两边扩展,进行比较更新
while (k < s.size()- && j > && s[k + ] == s[j - ]) { ++k; --j; } // Expand.
int new_len = k - j + ;
if (new_len > max_len) { min_start = j; max_len = new_len; }
}
return s.substr(min_start, max_len);
}
};
3、Manacher算法
俗称“马拉车算法”,是在中心扩展法的基础上,优化确定最大长度的算法;
专门设定长度数组(假设为p[len]),记录每个位置的最大长度;
为了避免长度奇偶问题,在原字符串的中间,插入‘#’异常符号;
举个例子:
s="abbahopxpo"
转换为
s_new="$#a#b#b#a#h#o#p#x#p#o#"
有较为形象具体的说明:https://segmentfault.com/a/1190000008484167
实现代码:
string add_string(string s){
string news="$#";
int len=s.size();
int j=;
for(int i=;i<len;i++){
news+=s[i];
news+='#';
}
return news;
}
class Solution {
public:
string longestPalindrome(string s) {
s=add_string(s);
int len=s.size(),maxlen=-;
int id,mx=,p[len],maxindex;
for(int i=;i<len;i++){
if(i<mx) {p[i]=min(p[*id-i],mx-i);
}else{p[i]=;} while(s[i-p[i]]==s[i+p[i]]) p[i]++;
if(mx<i+p[i]){
id=i;
mx=i+p[i];
}
if(maxlen<p[i]-){
maxlen=p[i]-;
maxindex=i;
}
}
string result="";
for(int i=maxindex-maxlen;i<=maxindex+maxlen;i++){
if(s[i]!='#'&&s[i]!='$'){
result+=s[i];
}
}
return result;
}
};
Leetcode0005--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 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- lintcode :Longest Palindromic Substring 最长回文子串
题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 【翻译】Longest Palindromic Substring 最长回文子串
原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...
- [leetcode]5. Longest Palindromic Substring最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 转载-----Java Longest Palindromic Substring(最长回文字符串)
转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...
- Longest Palindromic Substring (最长回文字符串)——两种方法还没看,仍需认真看看
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- Ubuntu网卡设置:配置/etc/netplan
对于Ubuntu1804版本,经过测试如下配置可以设置静态IP地址: Google@ubuntu:~$ cat /etc/netplan/01-netcfg.yaml network: etherne ...
- npm和gulp学习
npm的使用 node Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,是一种JavaScript语言运行平台,和浏览器这个运行平台是同一个概念. npm np ...
- 该页必须使用安全性较高的Web 浏览器查看
当用https访问某个网站时,IE提醒“该页必须使用安全性较高的Web 浏览器查看” 您要访问的资源使用了128位版本的“安全套接层(SSL)” 安全保护.要查看该资源,您需要使用支持该版本的SSL浏 ...
- FOJ2250 不可能弹幕结界
Problem 2250 不可能弹幕结界 Time Limit: 1000 mSec Memory Limit : 65536 KB Problem Description 咲夜需要穿过一片弹幕 ...
- oracle数据库审计
Oracle使用大量不同的审计方法来监控使用何种权限,以及访问哪些对象.审计不会防止使用这些权限,但可以提供有用的信息,用于揭示权限的滥用和误用. 下表中总结了Oracle数据库中不同类型的审计. 审 ...
- 清北学堂模拟赛d6t2 刀塔
分析:看到最小值最大就很显然是二分了吧,二分一下最小值,把小于它的数给删掉,然后看每个数向左边能延伸多长,往右边能延伸多长,最后统计一下有没有可行答案就可以了. #include <cstdio ...
- python浅拷贝与深拷贝
今天写程序,人为制造了一个由浅拷贝引起的bug,有必要归纳一下.先附上源代码: class PerformanceTest(object): def __init__(self): ....... s ...
- 利用Date类计算生活时间
今天学习到了Date类还有其他一些常用类! 这里就简单使用Date及其一些方法计算生活时间. import java.text.ParseException; import java.text.Sim ...
- applicationcontext理解使用
Spring ApplicationContext 容器 Application Context 是 spring 中较高级的容器.和 BeanFactory 类似,它可以加载配置文件中定义的 bea ...
- jvm的运行模式 client和 server两种
jvm的运行模式 client和 server两种 学习了:https://www.cnblogs.com/fsjohnhuang/p/4270505.html 在jdk 9的情况下,好像没有clie ...