最长回文子串

2017-04-08  本文已影响12人  水瓶鱼

题目

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Python

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: str
        """
        long_str=''
        longest=0
        for i in range(len(s)):
            for j in range(len(s),i,-1):
                if(j-i<longest):
                    break
                if self.is_palindrome(s[i:j]):
                    if j-i>longest:
                        long_str=s[i:j]
                        longest=j-i
                    break
        return long_str

    def is_palindrome(self,s):
        reverse_str=''
        for i in range((len(s))//2):
            if s[i]!=s[-i-1]:
                return False
        return True
上一篇 下一篇

猜你喜欢

热点阅读