5. 最长回文子串
5. 最长回文子串
方法一
class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ if s == "": return "" memo1 = [] memo2 = [] for (i,t) in enumerate(s): for j in range(i+1, len(s)): if s[len(s)+i-j] == t: if s[i:len(s)-j+i+1] == s[i:len(s)-j+i+1][::-1]: memo1.append([i,len(s)-j+i+1]) memo2.append(len(s)-j+1) break #return s[i:len(s)-j+i+1] if memo1 == []: return s[0] else: a = memo1[memo2.index(max(memo2))] return s[a[0]:a[1]]
方法二:Manacher 算法