题解 | #最长回文子串#双指针解法

最长回文子串

https://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param A string字符串 
# @return int整型
#双指针
#for 0 <= i < len(s):
#    找到以 s[i] 为中心的回文串
#    找到以 s[i] 和 s[i+1] 为中心的回文串
#    更新答案

class Solution:
    def ifhuiwen(self,s,l,r):
        while l>=0 and r<len(s) and s[l]==s[r]:
            l-=1
            r+=1
        return s[l+1:r]
    def getLongestPalindrome(self , A: str) -> int:
        num=0
        res=0
        for i in range(len(A)):
            res1=len(self.ifhuiwen(A,i,i))
            res2=len(self.ifhuiwen(A,i,i+1))
            num=max(res1,res2)
            res=max(res,num)
          
        return res


        # write code here

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务