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 ...
随机推荐
- 记录一下最近开发web移动前端的过程
两个项目 第一个是公司网站的移动端,我所在的公司是做某方面的新闻站的. 所以说页面基本是以一条条的新闻+图文混排为主,顶部有一个自动slider+触屏滑动的功能, 使用的是swipe插件,轻量,简洁非 ...
- javaScript中将时间戳转换成日期格式
function DateFormt(time, format) { ); var o = { , "d+": testDate.getDate(), "h+" ...
- WIN8 WIN10系统如何完全获取用户管理员权限
按住WIN+R 2 计算机配置----Windows设置----安全设置----本地策略----安全选项----用户账户控制:以管理员批准模式运行所有管理员,把启用改为禁止然后重启电脑
- 使vim中Syntastic支持C++11
安装好Syntastic后发现不支持c++11,会提示错误incompatible with c++98,解决方法如下: .vimrc中加入: let g:syntastic_cpp_compiler ...
- C程序设计语言练习题1-6
练习1-6 验证表达式getchar() != EOF的值是0还是1. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() // 定义名为ma ...
- Spring boot构建基于rest的Web服务
一.介绍:使用Spring Boot我们可以很容易的创建一个可独立运行的Rest web服务,其中内嵌tomact,我们只需“run”就可以查看效果了. Spring Boot利用Gradle或Mav ...
- Reshaper 快捷键
配置单个键盘快捷键 1.在主菜单上,选择Tools | Options 2.在导航视图中选择Environment | Keyboard 3.在命令列表中,选择启动ReSharper的命令名称.浏览列 ...
- 勾选checkbox之后,button按钮可用
js:function chkClick() { if (document.getElementById("chkBox").checked == true) { ...
- 关于API的设计和需求抽象
一,先来谈抽象吧,因为抽象跟后面的API的设计是息息相关的 有句话说的好(不知道谁说的了):计算机科学中的任何问题都可以抽象出一个中间层就解决了. 抽象是指在思维中对同类事物去除其现象的.次要的方面, ...
- mv,Directory not empty不能目录覆盖
一.mv /test1/* /test2/test1rm -rf /test1 二. You can however use rsync with the --remove-source-files ...