代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
@param A string字符串
@return int整型
class Solution: def getLongestPalindrome(self , A: str) -> int: # write code here maxlen = 1
for cur in range(1, len(A)):
left, right = cur-1, cur+1
tmp_len = 1
#先确定中心元素,以及左右扩散开始的下标。
while left >= 0 and A[left] == A[cur]:
left -= 1
tmp_len += 1
while right <= len(A)-1 and A[right] == A[cur]:
right += 1
tmp_len += 1
maxlen = max(maxlen, tmp_len)
#左右扩散
while left >= 0 and right <= len(A)-1:
if A[left] == A[right]:
left -= 1
right += 1
tmp_len += 2
else:
break
maxlen = max(maxlen, tmp_len)
return maxlen