[leetcode] Longest Palindromic Substring 多种解法

很经典的题目,求字符串中的最长回文子串。

(1)最朴素的解法 ---暴力 复杂度O(N3)

这也是最容易想到的方法,最外层循环枚举起点i,第二层循环从i+1开始向后枚举,第三层判断是不是回文串。最后取最长子串的返回。

代码比较简单,这里没有列出。

(2)中心扩展法。复杂度O(N2)

枚举每一个字符作为中心点向左右扩展。但是这里要注意,对于每一次扩展要分奇偶两种情况。否则可能会漏掉情况。

一发现不满足的情况马上break进行下一个中心字符的判断,代码如下:

class Solution {
public:
    string longestPalindrome(string s) {
        string ans;
        int len=s.length();
        int maxLength=-1,CurLen,Sbegin;
        for(int i=0;i<len;i++)
        {
                int left=i-1,right=i+1;
                while(left>=0&&right<len&&s[left]==s[right])//奇数情况
                {
                        CurLen=right-left+1;
                        if(CurLen>maxLength)
                        {
                        	maxLength=CurLen;
                        	Sbegin=left;
                        }
                        left--,right++;
                }

                left=i,right=i+1;
                while(left>=0&&right<len&&s[left]==s[right])//偶数情况
                {
                        CurLen=right-left+1;
                        if(CurLen>maxLength)
                        {
                        	maxLength=CurLen;
                        	Sbegin=left;
                        }
                        left--,right++;
                }

        }
        ans=s.substr(Sbegin,maxLength);
        return ans;

    }
};

(3)Manacher算法,复杂度只有O(n)

具体算法介绍:点击打开链接

附上精巧的代码:

class Solution {
public:
   string preProcess(string s) {
     int  n = s.length();
     if (n == 0) return "^$";
     string ret = "^";
     for (int i = 0; i < n; i++)
        ret += "#" + s.substr(i, 1);
     ret += "#$";
    return ret;
}  

string longestPalindrome(string s) {
     string T = preProcess(s);
     int n = T.length();
     int *P = new int[n];
     int C = 0, R = 0;
     for (int i = 1; i < n-1; i++) {
    int i_mirror = 2*C-i; // equals to i' = C - (i-C)  

    P[i] = (R > i) ? min(R-i, P[i_mirror]) : 0;  

    // Attempt to expand palindrome centered at i
    while (T[i + 1 + P[i]] == T[i - 1 - P[i]])
      P[i]++;  

    // If palindrome centered at i expand past R,
    // adjust center based on expanded palindrome.
    if (i + P[i] > R) {
      C = i;
      R = i + P[i];
    }
  }
  // Find the maximum element in P.
  int maxLen = 0;
  int centerIndex = 0;
  for (int i = 1; i < n-1; i++) {
    if (P[i] > maxLen) {
      maxLen = P[i];
      centerIndex = i;
    }
  }
  delete[] P;  

  return s.substr((centerIndex - 1 - maxLen)/2, maxLen);
}
};

(4)后缀树的解法,待补充

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 20:29:54

[leetcode] Longest Palindromic Substring 多种解法的相关文章

[LeetCode] Longest Palindromic Substring [14]

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length ofS is 1000, and there exists one unique longest palindromic substring. 原题链接 解题思路 最长回文字串,相信做过Palindrome Partitioning II 这个题的同学应该可以很快做出来.没错,这个题还可以

LeetCode &quot;Longest Palindromic Substring&quot; - 1D DP

2D DP is an intuitive solution of course, but I got an MLE error, so I simplified it into a 1D DP: class Solution { public: void goDp(vector<int> &dp, int &maxLen, int &is, int &ie, int startLen, int len, string &s) { for(int ile

Leetcode: Longest Palindromic Substring. java

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. 动态规划public class Solution { public String longestPalindrome(String s) { if

Leetcode:Longest Palindromic Substring之详细分析

原题链接:https://leetcode.com/problems/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]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. 基本思想: 本题的navie解法是对每个(i,j) i<=j 测试是否是回文串,并记录最大的情况.O(n^3) 如果从回文串的中心考虑往两

[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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

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,找出当中的最长回文字符子串. 1.枚举全部子串,并推断是否是回文串同一时候记录最大长度.超时. //找出全部子串,并推断是否是回文串

[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. http://fisherlei.blogspot.com/2012/12/leetcode-longest-palindromic-substrin

leetcode longest palindromic substring (medium) /java

最长回文字串 上题: 测试用例中,注意aaabaaaa. 但是我time limit exceeded.用了极暴力的方法解.(三层循环)找每一个字符的最长回文字串. 1 /** 2 * 最长回文子串 3 * 2017-5-7 4 **/ 5 6 import java.io.*; 7 import java.util.*; 8 import java.lang.*; 9 10 public class Solution 11 { 12 public static String longestPa