最长回文子串

最长回文子串

题目:

代码

class Solution:
    def longestPalindrome(self, s: str) -> str:
        length = len(s)
        alist = []

        for i in range(length):
            for j in range(i, length):
                alist.append(s[i:j + 1])
        alist.sort(key=lambda x : len(x))
        n = -1
        while True:
            qqs = alist[n]
            if qqs == qqs[::-1]:
                return qqs
            n -= 1
p = Solution()
print(p.longestPalindrome('babad'))
posted @ 2021-01-27 09:50  豆子V  阅读(39)  评论(0编辑  收藏  举报