题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
class Solution:
def getLongestPalindrome(self, A: str) -> int:
n = len(A)
if n < 2:
return n
max_length = 1
def expandAroundCenter(left: int, right: int) -> int:
while left >= 0 and right < n and A[left] == A[right]:
left -= 1
right += 1
return right - left - 1
for i in range(n):
len1 = expandAroundCenter(i, i) # 单字符作为中心
len2 = expandAroundCenter(i, i + 1) # 两个字符之间的空隙作为中心(针对偶数长度的回文子串)
max_length = max(max_length, len1, len2)
return max_length


腾讯音乐娱乐集团公司福利 285人发布