LeetCode 5 Longest Palindromic Substring (Python)

Posted by 小明MaxMing on January 19, 2021

题目

Given a string s, return the longest palindromic substring in s.

解题思路

枚举回文字符串中间的位置,向两边进行拓展,如果剩余长度小于等于当前最优的一半,结束循环,同时可以跳过连续相同的字母,使他们一起作为回文字符串的中间部分

代码

class Solution:
    def longestPalindrome(self, s: str) -> str:
        l = len(s)
        if l < 2:
            return s
        i = st = maxl = 0
        while i < l:
            if l - i <= maxl / 2:
                break
            j = k = i
            while k < l - 1 and s[k + 1] == s[k]:
                k += 1
            i = k + 1
            while k < l - 1 and j > 0 and s[k + 1] == s[j - 1]:
                k += 1
                j -= 1
            tmp = k - j + 1
            if tmp > maxl:
                st = j
                maxl = tmp
        return s[st: st + maxl]

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅