题目描述
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example 2:
Input: “cbbd”
Output: “bb”
代码解析
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
ret=''
for i in xrange(len(s)):
str_hw=self.huiwen(s,i,i)
if len(str_hw)>len(ret):
ret=str_hw
str_hw=self.huiwen(s,i,i+1)
if len(str_hw)>len(ret):
ret=str_hw
return ret
def huiwen(self, s ,i ,j ):
while i>=0 and j<=len(s)-1 and s[i]==s[j]:
i-=1
j+=1
return s[i+1:j]