【Leetcode】 Longest Palindromic Substring

来源:互联网 发布:c语言随机数 编辑:程序博客网 时间:2024/06/03 19:31

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

Example:

Input: "babad"Output: "bab"Note: "aba" is also a valid answer.

Example:

Input: "cbbd"Output: "bb"
class Solution:    # @return a string    def longestPalindrome(self, s):        if len(s)==0:        return 0        maxLen=1        start=0        for i in xrange(len(s)):        if i-maxLen >=1 and s[i-maxLen-1:i+1]==s[i-maxLen-1:i+1][::-1]:        start=i-maxLen-1        maxLen+=2        continue        if i-maxLen >=0 and s[i-maxLen:i+1]==s[i-maxLen:i+1][::-1]:        start=i-maxLen        maxLen+=1        return s[start:start+maxLen]