题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param A string字符串 # @return int整型 # class Solution: def getLongestPalindrome(self , A: str) -> int: cd = 1 for i in range(0,len(A)): for j in range(i+1+cd,len(A)+1): if A[i:j]==A[i:j][::-1]: cd=len(A[i:j]) return cd
剪枝双循环,比较容易理解,而且效率有时候也很高,最长回文序列的位置如果靠前的话时间复杂度会接近O(n)