LeetCode 5_Longest Palindromic Substring 

题目描写叙述:

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. 也即:求字符串中最长回文子串。

回文是什么我就不多少了。能够百度下!

方法一:暴力法(O(n^3))

两层循环扫描字符串的全部子串,之后推断选出的字符子串是否是回文,若是则看其长度!

代码例如以下:

class Solution {
public:
string longestPalindrome(string s)
{
// 暴力法O(n^3)
int n = s.size();
if (n == 0 || n == 1)
return s;
int maxLength = 1;
int k1 = 0, k2 = 0;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int k = 0;
int sign = 0;
while (k < (j - i + 1)/2 && s[i + k] == s[j - k])
k++;
if(k == (j - i + 1) / 2 )
{
sign = 1;
if (j - i + 1 > maxLength)
{
maxLength = j - i + 1;
k1 = i;
k2 = j;
if (maxLength == n - i)
return s.substr(k1,k2+1);
}
}
}
}
return s.substr(k1,k2+1);
}

不用说,肯定超时。显然暴力法有非常大的优化空间。在推断子串的时候肯定有非常多反复的情况,能够用一个表记录已经推断的情况!

因为题目说能够假定字符串的长度不超过1000,所以建立一个table[1000][1000] 的bool表,初始化为false。如果某子串(如果 i 到 j )为回文。令table[ i ][ j ]为true。之后推断的时候先查表和更新表。代码例如以下:

class Solution {
public:
string longestPalindrome(string s)
{
int n = s.length();
if(n == 0 || n == 1)
return s;
int maxLength = 1;
int palindromBegin = 0;
bool table[1000][1000] = {false};
for(int i = 0; i < n; i++)
table[i][i] = true;
for (int i = 0; i < n; i++)
if(s[i] == s[i + 1])
{
table[i][i + 1] = true;
maxLength = 2;
palindromBegin = i;
}
for (int len = 3; len <= n ; len++)
{
for (int i = 0; i < n - len + 1; i++)
{
int j = i + len - 1;
if (s[i] == s[j] && table[i + 1][j - 1] == true)
{
table[i][j] = true;
maxLength = len;
palindromBegin = i;
}
}
}
return s.substr(palindromBegin, maxLength);
}

上面的方法时间复杂度为O(n^2),能够满足题目的要求。

事实上还能够考虑回文的中心点。向两边扩展(回文的中心点能够是摸个字符。也能够是某两个字符的中间),代码例如以下:

string expandAroundCenter(string s, int c1, int c2) {
int l = c1, r = c2;
int n = s.length();
while (l >= 0 && r <= n-1 && s[l] == s[r]) {
l--;
r++;
}
return s.substr(l+1, r-l-1);
}
class Solution {
public:
string longestPalindrome(string s)
{
int n = s.length();
if (n == 0) return "";
string longest = s.substr(0, 1); // a single char itself is a palindrome
for (int i = 0; i < n-1; i++) {
string p1 = expandAroundCenter(s, i, i);
if (p1.length() > longest.length())
longest = p1; string p2 = expandAroundCenter(s, i, i+1);
if (p2.length() > longest.length())
longest = p2;
}
return longest;
}
};

代码的复杂度为O(n^2)。另一种说复杂度为O(n)的方法,只是我没去看,有兴趣的能够看下: http://www.cnblogs.com/bitzhuwei/p/Longest-Palindromic-Substring-Part-II.html。

LeetCode 5_Longest Palindromic Substring的更多相关文章

  1. LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium

    题目: Given a , and there exists one unique longest palindromic substring. 解题思路:1.简单思路:暴力破解法,时间复杂度O(n^ ...

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

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

  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(manacher algorithm)

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

  5. C++ leetcode Longest Palindromic Substring

    明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...

  6. Leetcode:Longest Palindromic Substring分析和实现

    问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...

  7. Leetcode: Longest Palindromic Substring && Summary: Palindrome

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

  8. LeetCode:Longest Palindromic Substring 最长回文子串

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

  9. Leetcode: Longest Palindromic Substring. java

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

随机推荐

  1. springboot创建项目

    Springboot作为轻量级快速开发受到无数java人的青睐,Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过 ...

  2. A - Word

    Problem description Vasya is very upset that many people on the Net mix uppercase and lowercase lett ...

  3. vs2008 启动IE浏览器 出现DW20.exe占用大量cpu 服务器iis 异常调试

    DW20.exe占用大量cpu 服务器iis运行出现异常想查一下故障原因,发现有好几个DW20.exe进程,每个占用20%左右的cpu,在任务管理器中将其终止后,它又自动运行起来了 查了一下DW20. ...

  4. C#之密封类(详解)

    10.3  密封类与密封方法 如果所有的类都可以被继承,那么很容易导致继承的滥用,进而使类的层次结构体系变得十分复杂,这样使得开发人员对类的理解和使用变得十分困难,为了避免滥用继承,C#中提出了密封类 ...

  5. matplotlib之pyplot 学习示例

    现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子.  1. 简单的绘图( ...

  6. 【Oracle】删除手工创建的数据库

    众所周知,DBCA创建的数据库可以通过DBCA命令删除,但是手工创建的数据库却不能用此方式删除,下面给出删除方式: SQL> startup mount exclusive SQL> al ...

  7. linux挂载ntfs格式的U盘

    1.需要安装一个ntfs-3G工具 工具包下载网站:http://www.tuxera.com/community/ntfs-3g-download/ 根据情况选择要下载的包. 2.上传到Linux服 ...

  8. 图像连通域检测的2路算法Code

    本文算法描述参考链接:http://blog.csdn.net/icvpr/article/details/10259577 两遍扫描法: (1)第一次扫描: 访问当前像素B(x,y),如果B(x,y ...

  9. 读书笔记「Python编程:从入门到实践」_5.if语句

    5.1 一个简单示例 cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars: if car == 'bmw': print(car.up ...

  10. mysql手册操作

    1.show table status   显示表状态 2.VERSION()   版本:CURRENT_DATE   当前日期: NOW()   当前时间:USER   当前用户 3.GRANT A ...