首页 > 编程语言 > 详细

leetcode-python-最长回文串

时间:2021-06-14 17:42:18      阅读:20      评论:0      收藏:0      [点我收藏+]

1)逐个遍历,向两边扩散。分单一扩散(奇数),和双字符扩散(偶数)两种情况考虑

时间复杂度较高

class Solution:
    def longestPalindrome(self, s: str) -> str:
        length = len(s)
        maxlength = 0
        for i in range(length):
            l = i
            r = i
            while l >= 0 and r < length and s[l] == s[r]:
                if maxlength < len(s[l:r+1]):
                    maxlength = len(s[l:r+1])
                    result = s[l:r+1]
                l -= 1
                r += 1
            l = i
            r = i+1
            while l >= 0 and r < length and s[l] == s[r]:
                if maxlength < len(s[l:r+1]):
                    maxlength = len(s[l:r+1])
                    result = s[l:r+1]
                l -= 1
                r += 1
        return result

 

leetcode-python-最长回文串

原文:https://www.cnblogs.com/cbachen/p/14882666.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!