题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
#暴力,遍历所有子串 一个个比较 a == a[::-1] 是就记录长度,然后存储最长的子串
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param A string字符串
# @return int整型
#
class Solution:
def getLongestPalindrome(self , A: str) -> int:
# write code here
res = 1
if len(A) == 1 :
return res
else:
left = 0
i = 1
while i < len(A)+1 and left < len(A):
temp = A[left:i]
if temp == temp[::-1]:
res= max(res,len(temp))
elif i == len(A):
i =0
left += 1
i+=1
return res
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param A string字符串
# @return int整型
#
class Solution:
def getLongestPalindrome(self , A: str) -> int:
# write code here
res = 1
if len(A) == 1 :
return res
else:
left = 0
i = 1
while i < len(A)+1 and left < len(A):
temp = A[left:i]
if temp == temp[::-1]:
res= max(res,len(temp))
elif i == len(A):
i =0
left += 1
i+=1
return res