Longest Palindromic Substring -LeetCode
题目
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的更多相关文章
- Longest Palindromic Substring——LeetCode
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Longest Palindromic Substring leetcode java
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- [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 lengt ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【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 ...
- [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(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
随机推荐
- 实例讲解MySQL联合查询
好了终于贴完了MySQL联合查询的内容了,加上上一篇一共2篇,都是我转载的,实例讲解MySQL联合查询.那下面就具体讲讲简单的JOIN的用法了.首先我们假设有2个表A和B,他们的表结构和字段分别为: ...
- JQuery this和$(this)的区别及获取$(this)子元素对象的方法
1.JQuery this和$(this)的区别 相信很多刚接触JQuery的人,很多都会对$(this)和this的区别模糊不清,那么这两者有什么区别呢? 首先来看看JQuery中的 $() 这 ...
- iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习
iOS开发 - 不进入待机(屏幕保持唤醒)---UIApplication学习 如果你不希望应用运行时 iPhone 进入锁屏待机状态,加入下面这行代码即可 [[UIApplication share ...
- build.prop修改详细说明
用RE进入/system/挂载读写,找到build.prop复制到/sdcarrd进行修改比较保险.也可以挂载读写后,直接选择用文本编辑器打开,进行编辑.乱改有风险,修改需谨慎.1.# begin b ...
- Pausing Coyote HTTP/1.1 on http-8080
一般情况下我看到8080便认为是端口占用的问题,其实不是,但是在任务管理器中并没有找到javaw.exe,只有javaservice.exe, 只有重启tomcat了,蓝后就好了......
- [每日一题JS] 正则表达式
判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /\b[a-zA-Z]{1}[a-zA-Z0-9_]{4,19}\b/; var fl ...
- 华为手机APK 汉语译名
华为桌面 删除前要找一个桌面程序代替短信息 不删(貌似自带短信会偷流量..猜测猜测.唉~暂时没办法)输入法 也是删除前要找到替代输入法,否则后果..... 同名的odex也删了,主体不见了留躯壳何用? ...
- Bugscan学习笔记------关于urlparse
urlparse模块主要是把url拆分为6部分,并返回元组.并且可以把拆分后的部分再组成一个url.主要有函数有urljoin.urlsplit.urlunsplit.urlparse等. ***** ...
- WPF多线程问题
最近碰到这种多线程问题都是在WPF项目中. 1. 问题是这样.有个一主界面线程,然后background线程启动,这个background线程试图去修改主界面里面的数据. 造成死锁. 调用过程,主界面 ...
- CentOS系统、Jdk、Tomcat安装实战
CentOS系统.Jdk.Tomcat安装实战 第一次接触Liunx系统,都说J2EE系统在Li ...