题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param A string字符串 # @return int整型 # class Solution: def getLongestPalindrome(self , A: str) -> int: # write code here n = len(A) ans = len(A[0]) if n == 1: return ans for i in range(n-1): for j in range(i+ans,n): if A[i] == A[j]: res = A[i:j+1] if res == res[::-1] and len(res) > ans: ans = len(res) return ans
最长回文子串,两层遍历,首先判断首尾是否一致,然后再判断是否回文即可,注意题目要求是返回长度 不是子串