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 ...
随机推荐
- 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。
使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...
- 解决APP中fragment重叠问题
由于内存重启,导致的frgament重叠,其原因就是FragmentState没有保存Fragment的显示状态,即mHidden,导致页面重启后,该值为默认的false,即show状态,所以导致了F ...
- ECSTORE验证码优化
用ecstore的朋友应该知道,ecstore的验证码超级鸡肋. 特别是字母和数字混合,根本就看不懂写的是什么? 数字还好,但是字母就别提了.而且还小. 索性就把验证码换掉.研究一下发现,ecstor ...
- XML DOM 遍历Xml文档
1.xml文档内容: <?xml version="1.0" encoding="utf-8" ?> <bookstore> <b ...
- Android String 转 MD5
/** * 将字符串转成16 位MD5值 * * @param string * @return */ public static String MD5(String string) { byte[ ...
- BZOJ1089: [SCOI2003]严格n元树
1089: [SCOI2003]严格n元树 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 762 Solved: 387[Submit][Status ...
- ops
consists several key projects separately stand-alone connected entities massive scalability massive ...
- 负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体
负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体 负重前行的婚纱线上路
- [置顶] API相关工作过往的总结之Sandcastle简要使用介绍
Sandcastle介绍 在微软推出Sandcastle之前,人们倾向于选择开源的NDoc(.NET代码文档生成器).NDo可以将 C#.NET 编译生成的程序集和对应的 /doc XML文档,自动转 ...
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...