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:
Example:
|
题目意思: 给出字符串中最长的回文串 若长度相同,给出位置最前的 |
作为较经典的题目,回文串通常有几种方法,已经有很多人分析过了,提供链接: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 ...
随机推荐
- Coefficient Computation (大整数、Java解决)
Coefficient Computation UVALive8265 题意:计算组合数C(n,k)的值并将值按给定的进制输出. 思路:Java大整数类硬上. PS:刚刚学完Java的大整数类,结果却 ...
- 爬虫文件存储-2:MongoDB
1.连接MongoDB 连接 MongoDB 我们需要使用 PyMongo 库里面的 MongoClient,一般来说传入 MongoDB 的 IP 及端口即可,第一个参数为地址 host,第二个参数 ...
- from __future__ import absolute_import的作用
关于这句from future import absolute_import的作用: 直观地看就是说"加入绝对引入这个新特性".说到绝对引入,当然就会想到相对引入.那么什么是相对引 ...
- 【codeforces 761E】Dasha and Puzzle
[题目链接]:http://codeforces.com/contest/761/problem/E [题意] 给你一棵树,让你在平面上选定n个坐标; 使得这棵树的连接关系以二维坐标的形式展现出来; ...
- ZooKeeper学习总结(1)——ZooKeeper入门介绍
1. 概述 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 它有如下的一些特点: 简单 Zookeeper的核 ...
- Caused by: android.os.TransactionTooLargeException总结
错误信息 Error: android.os.TransactionTooLargeException W/ActivityManager(344): android.os.TransactionTo ...
- angular5 httpclient的示例实战
摘要: 从angular 4.3.0 以后的版本开始使用httpclient,替换了之前的http,引用的包路径已经变为了angular/common/http了 一个基础的 httpclient 样 ...
- 开启IIS的动态gzip功能
首先安装IIS的动态压缩模块 然后打开system32/intesrv下的applicationhost文件,找到其中的webServer节点,将其中的压缩配置部分替换如下: <?xml ver ...
- ustc 1117
无根树同构 有两种方法,一种是确定其中一棵树,另一棵树枚举根节点. 一种是,利用拓扑排序,先确定其中一棵树.另一棵树,若拓扑后剩两个节点,则枚举这两个节点为根结点,否则,只需做一次.注意,无根树节点入 ...
- Java 两个整数相除保留两位小数,将小数转化为百分数
Java 两个整数相除保留两位小数,将小数转化为百分数 源于:http://blog.sina.com.cn/s/blog_624d755d0101cvuq.html 后来学习了:http://blo ...