leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
找出最大的回文子串,回文就是指字符串倒过来和之前的一样。例如aba 倒过来还是aba。abac中的最大回文子串就是aba。
我一开始想的是,start从第一个开始,right从后往前找,找到和start相同的字符时,判断是不是回文。是的话记录长度不再往前找,start++,依次类推。记录最长回文下标。最后返回。最后一个case提示Time limit exceede
class Solution {
private:
bool isPalindrome(string s)
{
bool flag = true;
int len = s.length();
if (len < )
return true;
int left = , right = len - ; while(left < right)
{
if (s[left] != s[right])
flag = false;
left++;
right--;
}
return flag;
}
public:
string longestPalindrome(string s)
{
if (s.length() < )
return s;
int len = s.length();
int right = len - , longest = ;
int index[] = {,}; for (int i = ; i < right - longest; i++)
{
for (int j = right; j > i + longest; j--)
{
if (s[i] == s[j] && (j - i + > longest) )
{
if(isPalindrome(s.substr(i, j - i + )))
{
index[] = i;
index[] = j;
longest = j - i + ;
break;
}
}
}
}
return s.substr(index[],longest);
}
};
考虑了下复杂的,如上的复杂的好像超过了n方,是n三方。
从中间向两边展开的方法可以实现n方。
class Solution {
//从中间向两边展开
string expandAroundCenter(string s, int c1, int c2) {
int l = c1, r = c2;
int n = s.length();
while (l >= && r <= n- && s[l] == s[r]) {
l--;
r++;
}
return s.substr(l+, r-l-);
} public:
string longestPalindrome(string s) {
int n = s.length();
if (n < ) return s;
string longest;
for (int i = ; i < n-; i++) {
string p1 = expandAroundCenter(s, i, i); //长度为奇数的候选回文字符串
if (p1.length() > longest.length())
longest = p1; string p2 = expandAroundCenter(s, i, i+);//长度为偶数的候选回文字符串
if (p2.length() > longest.length())
longest = p2;
}
return longest;
}
};
http://blog.csdn.net/feliciafay/article/details/16984031这位大牛详细分析了各种复杂度。包括O(n)
leetcode第五题--Longest Palindromic Substring的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- LeetCode(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
- LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
随机推荐
- android 渐变drawable
渐变Drawable它是使用<gradient>的标记的形状Drawable定义子节点的定义. 每个梯度Drawable求至少要有一个startColor和endColor属性,而且支持一 ...
- UVa 10190 - Divide, But Not Quite Conquer!
称号:给你第一个任期的等比数列和倒数公比,最后一个条目假定1这一系列的输出,否则输出Boring!. 分析:数学.递减的.所以公比的倒数一定要大于1.即m > 1. 然后在附加一个条件n &g ...
- Advanced Installer 制作.NetWeb部署安装包
原文:Advanced Installer 制作.NetWeb部署安装包 因为是.Net的Web应用程序,所以想用Advanced Installer 调用Dll实现安装部署. 因为我需要自己定制参数 ...
- 在.NET Fiddle有趣的沙盒代码
在.NET Fiddle有趣的沙盒代码 笔者:Tony Patton | 托尼·巴顿译:PurpleEndurer,2014-11-18,第1版 C#和VB.NET开发者能够使用.NET Fiddle ...
- 国产与第三方库FFmpeg SDK
一个.编译并安装第三方库 1. libfaac # tar -zxvf faac-1.28.tar.gz # cd faac-1.28 # ./configure --prefix=/opt/YOUR ...
- Android - 警告:it is always overridden by the value specified in the Gradle build script
警告:it is always overridden by the value specified in the Gradle build script 本文地址: http://blog.csdn. ...
- codeigniter 操作mysql的PHP代码--更新
支持标准前缀 1)查询没有平等,有平等的 $this->db->get_where('host',array('host'=>'ddd','id !='=>0))->ro ...
- C#按LastID进行分页——与lambda形成链式
public static class PageHelper { /// <summary> /// 按页码分页 /// </summary> /// <param na ...
- .NET中IDisposable接口的基本使用
首先来看MSDN中关于这个接口的说明: [ComVisible(true)] public interface IDisposable { // Methods void Dispose(); } 1 ...
- Redhat Linux挂载NTFS格式的移动硬盘
Redhat Linux挂载NTFS格式的移动硬盘 1. 选择下载ntfs-3g的源码包或rpm包 http://www.tuxera.com/community/open-source-ntfs-3 ...