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. flash 遮住 div 解决办法

    被遮盖的div 下面的代码   <!--列表菜单-->             <div id="opreationmenu" style="posit ...

  2. Hive搭建与简单使用

    hive搭建与简单使用(1) 标签(空格分隔): hive,mysql hive相当于编译器的组件,他并不存储数据,元数据存储在mysql中,数据则存放在hdfs中,通过hive,可以利用sql语句对 ...

  3. 【Codeforces】Codeforces Round #373 (Div. 2) -C

    C. Efim and Strange Grade Efim just received his grade for the last test. He studies in a special sc ...

  4. sql的padleft

    /* SELECT dbo.fn_PadLeft('8', '0', 6) */ create function fn_PadLeft(@num nvarchar(16),@paddingChar c ...

  5. vim的快捷键大全

    vim是开发利器,掌握快捷可以事半功倍,这里总结下常用的快捷键,提高开发速度这里写代码片 1.vim ~/.vimrc 进入配置文件 如果不知道vimrc文件在哪,可使用 :scriptnames 来 ...

  6. python tips:dict的key顺序

    python3.6+版本中,dict的键值保持插入有序. t = list(range(10)) b = t[:] d = dict(zip(t, b)) print(list(d.items())) ...

  7. matlab学习下拉菜单

    用matlab添加listbox控件 修改string和value值,value为几就对应第几行字符串 添加button按钮,将string值改为“选择x轴参数”,字体大小为10 再添加一个按钮,将s ...

  8. Windows数字代码签名的作用和流程

    什么是数字代码签名?数字签名代码是一种技术,它使用数字证书来识别软件的发布商和使用hash算法来确保软件的完整性.数字签名使用公共密匙签名书法被创建,它使用两种不同的密匙:公共密匙和私有密匙,我们称其 ...

  9. 终于等到你!微软正式上线 Windows Terminal 预览版

    前一段时间,一直在知乎.技术社区收到技术小伙伴们的终极拷问:微软Build 大会上提到的**6月中旬**要上Windows store 的 Windows Terminal 到底啥时候可以用到呀? 有 ...

  10. POJ3253 Fence Repair【贪心】

    我们的小伙伴Bingo真的很调皮,他在上课的路上看到树上有个鸟窝,他就想去把他捅下来,但是鸟窝很高他够不到,于是他就到处找木棍,想把这些木棍接在一起,然后去捅鸟窝.他一共找了N跟木棍 (1 ≤ N ≤ ...