题解 | #最长回文子串#

最长回文子串

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

注意return hi-lo-1,其实是hi-lo-2+1

# -*- coding:utf-8 -*-

class Solution:
    def getLongestPalindrome(self, A, n):
        # write code here
        def LongestPalindrome(s, lo, hi):          
            while lo >= 0 and hi < len(s) and s[lo] == s[hi]:
                lo -= 1
                hi += 1
            return hi-lo-1
        res = 0
        for i in range(n-1):
            res = max(res, LongestPalindrome(A, i, i))
            res = max(res, LongestPalindrome(A, i, i+1))
        return res
全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务