美文网首页
05.leetcode题目讲解(Python):最长回文子串

05.leetcode题目讲解(Python):最长回文子串

作者: 夏山闻汐 | 来源:发表于2018-06-28 17:41 被阅读116次

题目:


image.png

提供一个比较容易想到的解法,主要思路是利用滑动窗口,参考代码如下:

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        s = s
        mlen = len(s)
        while True:
            i = 0
            while i + mlen <= len(s):
                sl = s[i:i + mlen]
                sr = sl[::-1]
                if sl == sr:
                    return sl
                i = i + 1
            mlen = mlen - 1
            if mlen == 0:
                return "No solution"

相关文章

网友评论

      本文标题:05.leetcode题目讲解(Python):最长回文子串

      本文链接:https://www.haomeiwen.com/subject/reknyftx.html