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. Jmeter - 服务器性能检测

    在对系统做压力测试时,往往需要对服务的性能进行监控,包括CPU,Memory,IO,还有网络情况进行监控. Jemter有个一插件,能很好的支持这些性能监控.原理是服务器启动服务之后,测试机发起请求, ...

  2. [你必须知道的.NET]目录导航

    http://www.cnblogs.com/anytao/archive/2007/09/14/must_net_catalog.html

  3. ie9长度兼容

    onchange="this.value=this.value.substring(0, 10)" onkeydown="this.value=this.value.su ...

  4. 如何通过putty软件远程登录并且控制linux平台

    准备备工作: 下载putty远程登录软件,图标如下 打开linux主机. Linux主机准备条件: 1 配置IP ,如果大家使用虚拟机的话建议通过vm1或者vm8进行与本地真实机进行连接,同时注意要避 ...

  5. JQuery特效之心形图片墙

    效果如图: 代码如下: <!doctype html> <html lang="en"> <head> <meta charset=&qu ...

  6. Java中String类的常用方法

    判断功能的方法 public boolean equals (Object anObject) :将此字符串与指定对象进行比较. public boolean equalsIgnoreCase (St ...

  7. 阿里P7架构师详解微服务链路追踪原理

    背景介绍 在微服务横行的时代,服务化思维逐渐成为了程序员的基本思维模式,但是,由于绝大部分项目只是一味地增加服务,并没有对其妥善管理,当接口出现问题时,很难从错综复杂的服务调用网络中找到问题根源,从而 ...

  8. javeee 字节Buffered

    package Zy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...

  9. Git创建本地分支并关联远程分支(一)

    默认,git项目只有一个分支,就是master,我们当然可以在本地创建多个分支,并推送到远程git管理平台上,或者将远程git管理平台上的其他分支拉取到自己电脑上. 一.查看本地已有的分支 进入到项目 ...

  10. PAT_A1136#A Delayed Palindrome

    Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...