题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param A string字符串 # @return int整型 # class Solution: def check(self,n): if n==n[::-1]: return True else: return False def getLongestPalindrome(self , A: str) -> int: # write code here dp = [i for i in range(len(A))] for i in range(1,len(A)): this = A[i] lastleft = dp[i-1] if i-1-lastleft+1>1: if lastleft>0: if this==A[lastleft-1]: dp[i]=lastleft-1 if dp[i]==i: for k in range(lastleft,i,1): if self.check(A[k:i+1]): dp[i]=k break else: if i-1>0: if this==A[i-2]: dp[i]=i-2 if this==A[i-1]: dp[i]=min(dp[i],i-1) dp[i] = min(dp[i],i) maxc = 0 for i in range(len(A)): now = i-dp[i]+1 if now>maxc: maxc=now return maxc