题目

Given a string s,find the longest palindromic substring in S.You may assume  that the maximum length of S is 1000,and there exist one unique longest palindromic substring.

分析与解法

如果一段字符串是回文,那么以某个字符为中心的前缀和后缀都是相同的.例如,一段回文串"aba",以b为中心,它的前缀与后缀都是相同的.

因此,我们可以枚举中心位置,然后在该位置上向左右两边扩展,记录并更新得到的回文长度.代码如下:

strng LongestPalidrome(string s){
int i,j,max,c;
max=; string ret; for(i=;i<s.size();i++){
for(j=;(i-j>=) && (i+j<n);j++){ //假设只是奇数形式的字符串
if(s[i-j]!=s[i+j])
break; c=j*+;
} if(c>max) {
ret=s.substr(i-j+,c);
max=c;
} for(j=;(i-j>=) && (i+j+<n);j++){ //假设这是偶数形式的字符串
if(s[i-j]!=s[i+j+])
break; c=j*+;
} if(c>max) {
ret=s.substr(i-j+,c);
max=c;
} return ret;
}

它们分别对于以i中心的,长度为奇数与偶数的两种情况.时间复杂度为O(n^2).

在网上还有一种O(N)时间复杂度的算法,比较复杂.可以参考:http://blog.csdn.net/feliciafay/article/details/16984031

Longest Palindromic Substring -LeetCode的更多相关文章

  1. Longest Palindromic Substring——LeetCode

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  2. Longest Palindromic Substring leetcode java

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    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 5. Longest Palindromic Substring

    写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...

  6. LeetCode 5 Longest Palindromic Substring(最长子序列)

    题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...

  7. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  8. [LeetCode] Longest Palindromic Substring(manacher algorithm)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

随机推荐

  1. css3画苹果logo

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. CSS - 针对不同浏览器的写法

    火狐浏览器: @-moz-document url-prefix() { /*这段区域的样式代码只在firefox起作用*/ .x{ width: 100px; height: 100px; back ...

  3. Python新手学习基础之运算符——位运算

    位运算符 位运算实际上是把数字看作二进制来进行计算,它的运算法则如下: 结合实例,来看下位运算是如何进行的吧: 位运算在实际应用中用途很广泛,比如我们经常听到的子网掩码,它其实就是和IP地址做了按位与 ...

  4. About abstract class.

    Abstract means should be realized. Virtual means could be overrided. It is very different!

  5. Ubuntu 怎么在右键添加打开终端

    方法一: 搜索nautilus-open-terminal安装 命令行:sudo apt-get install nautilus-open-terminal        (如果提示为找的什么的就s ...

  6. activiti 5.17 流程图中文乱码问题

    1. 流程图中任务中的中文乱码显示问题.   解决方法:设置processEngineConfiguration中的两个字体属性,例如: <bean id="processEngine ...

  7. MySQL 学习笔记 (范式)

    范式基本就是不要有重复的数据,表和表之间都是用主键和外键来联系 表的关系通常分3中 1 对 1 1 对 多 多 对 多 多 对 多 是用另一个表来实现的,这个表记入了a 表和 b表之间多对多的联系主键

  8. Cmake ,Out of Source Build

    Out of Source build呢,就是让Cmake产生的临时垃圾文件,不关乎于项目实际本身的文件放到一个目录里,一般我们把这个目录放在项目根目录(也可以认为是根CmakeLists.txt)下 ...

  9. Android开发多线程断点续传下载器

    使用多线程断点续传下载器在下载的时候多个线程并发可以占用服务器端更多资源,从而加快下载速度,在下载过程中记录每个线程已拷贝数据的数量,如果下载中断,比如无信号断线.电量不足等情况下,这就需要使用到断点 ...

  10. Ubuntu/Linux 笔记应用 为知笔记(支持markdown)

    发现网易云笔记没有Linux,但是为知笔记有Linux版本,且支持markdown格式 sudo add-apt-repository ppa:wiznote-team sudo apt-get up ...