题目链接:https://leetcode-cn.com/problems/longest-palindromic-substring/
代码:
1 class Solution(object): 2 def longestPalindrome(self, s): 3 """ 4 :type s: str 5 :rtype: str 6 """ 7 n = len(s) 8 9 m,l,r = 0,0,0 10 11 for i in range(n): 12 # odd case 13 for j in range(min(i+1,n-i)): 14 if s[i-j] != s[i+j]: 15 break 16 if 2*j + 1 > m : 17 m = 2 * j + 1 18 l = i-j 19 r = i+j 20 21 22 if i+1 < n and s[i] == s[i+1]: 23 for j in range(min(i+1,n-i-1)): 24 if s[i-j] != s[i+j+1]: 25 break 26 if 2 * j + 2 > m : 27 m = 2*j +2 28 l = i-j 29 r = i+j+1 30 return s[l:r+1]