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. BZOJ 4698 差分+后缀数组

    思路: 对所有序列差分一下 公共串的长度+1就是答案了 二分 扫一遍height即可,.. //By SiriusRen #include <cstdio> #include <cs ...

  2. Vue 区别

    computed和methods区别 效果是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值. 而methods,在重新渲染的时候,函数总会重新调用执行.

  3. .Net Core添加分布式Session

    一.Session HTTP是一个无状态协议,Web服务器将每一个请求都视为独立请求.并且不保存之前请求中用户的值. Session 状态是ASP.NET Core提供的一个功能,它可以在用户通应用访 ...

  4. 获取XML里指定的节点内容信息

    HttpContent bw = new StringContent(StrXml, Encoding.UTF8, "application/Xml"); var Msg = aw ...

  5. Java数据的基本类型

    整数类型: byte:8位(1个字节) eg:byte x=2,y: 错误实例:byte b:b=b+3:   其中b=b+3是错误的,应该是b=(byte)(b+3)强制转换: short:16位( ...

  6. 【sqli-labs】 less33 GET- Bypass AddSlashes (GET型绕过addslashes() 函数的宽字节注入)

    和less32一样,对关键字符进行了添加\ 关于addslashes()函数 payload和less32一样 http://192.168.136.128/sqli-labs-master/Less ...

  7. ssl_protocols和ssl_ciphers应该怎么配置

    http://wiki.nginx.org/HttpSslModule#ssl_ciphers 推荐配置: A) 在Apache 的 SSL 配置中禁用 SSLv3 和 SSLv3SSLProtoco ...

  8. 说说web缓存-强缓存、协商缓存

    网上关于WEB缓存的文章很多,今天汇总一下. 为什么要用缓存 一般针对静态资源如CSS,JS,图片等使用缓存,原因如下: 请求更快:通过将内容缓存在本地浏览器或距离最近的缓存服务器(如CDN),在不影 ...

  9. C# 遍历文本框

    #region 文本框指定位置加入回车符 private void button1_Click(object sender, EventArgs e) { #region // 查询首字母位置 //s ...

  10. C 语言复杂声明

    int board [8] [8] ; //声明一个内含 int 数组的数组 int ** ptr ; //声明一个指向指针的指针,被指向的指针指向 int int * risks [10] ; // ...