枚举中心点
class Solution:
def longestPalindrome(self, s: str) -> str:
res = ''
i = 0
while i < len(s):
j = i; k = i # 奇数情况,j,k为左右指针,起点相同
while j >= 0 and k < len(s) and s[j] == s[k]:
if len(res) < k - j + 1:
res = s[j : k + 1]
j -= 1
k += 1
j = i; k = i + 1 # 偶数情况,j,k为左右指针,起点不同
while j >= 0 and k < len(s) and s[j] == s[k]:
if len(res) < k - j + 1:
res = s[j : k + 1]
j -= 1
k += 1
i += 1
return res