Loading

[最大最长]最长回文子串

class Solution:
    def longestPalindrome(self, s: str) -> str:

        n = len(s)
        index = 0
        max_len = 1
        max_str = s[0]
        while index < n:

            left = math.floor(index)
            right = math.ceil(index)

            while left >= 0 and right < n and s[left] == s[right]:
                if right - left + 1 > max_len:
                    max_len = right - left + 1
                    max_str = s[left:right+1]
                left -= 1
                right += 1

            index += 0.5

        return max_str

posted @ 2024-09-18 10:58  Duancf  阅读(12)  评论(0)    收藏  举报