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 < 3)
        return true;
    int left = 0, right = len - 1;

    while(left < right)
    {
        if (s[left] != s[right])
            flag = false;
        left++;
        right--;
    }
    return flag;
}
public:
string longestPalindrome(string s)
{
    if (s.length() < 3)
        return s;
    int len = s.length();
    int right = len - 1, longest = 0;
    int index[] = {0,0};

    for (int i = 0; i < right - longest; i++)
    {
        for (int j = right; j > i + longest; j--)
        {
            if (s[i] == s[j] && (j - i + 1 > longest) )
            {
                if(isPalindrome(s.substr(i, j - i + 1)))
                {
                    index[0] = i;
                    index[1] = j;
                    longest = j - i + 1;
                    break;
                }
            }
        }
    }
    return s.substr(index[0],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 >= 0 && r <= n-1 && s[l] == s[r]) {
    l--;
    r++;
  }
  return s.substr(l+1, r-l-1);
}  

public:
string longestPalindrome(string s) {
  int n = s.length();
  if (n < 3) return s;
  string longest;
  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;
}
};

http://blog.csdn.net/feliciafay/article/details/16984031这位大牛详细分析了各种复杂度。包括O(n)

时间: 2024-10-23 08:06:45

leetcode第五题--Longest Palindromic Substring的相关文章

Leetcode第五题_Longest Palindromic Substring

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. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode【5】. Longest Palindromic Substring --java实现

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. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

Leetcode:【DP】Longest Palindromic Substring 解题报告

Longest Palindromic Substring -- HARD 级别 Question SolutionGiven 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. 经典的DP题目. 主页君给出3种解

LeetCode 题解之 5. Longest Palindromic Substring

5. Longest Palindromic Substring 题目描述和难度 题目描述: 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设?s 的最大长度为1000. 示例 1: 输入: "babad"输出: "bab"注意: "aba"也是一个有效答案. 示例 2: 输入: "cbbd"输出: "bb" 题目难度:中等. 英文网址:5. Longest Palindromic Substri

LeetCode第五题,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. 题意解析: 最长回文子串.就是给定一个字符串S,找出其中的最长回文子串,并返回该子串. 解法: 第一种方法显然是循环暴力枚举,复杂度为O(

LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 翻译: 给定一个字符串s,找出s中最长的回文子串.你可以假设s的最大长度是1000. 什么叫回文子串? 就是字符串中,满足能正读反读都一样的子串,就是回文子串.如下所示 Input: "babad"

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

LeetCode #5 Longest Palindromic Substring (M)

[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. [Analysis] 这题的思路有很多种,网上也有各种讨论.这里我采用的是个人觉得比较好理解的一种利用Dynamic Progra

Longest Palindromic Substring——LeetCode

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. 题目大意:给一个字符串S,存在唯一的最长的回文子串,求这个回文子串. 解题思路:首先这个题有O(n)的解,是在http://articles.leet