Python学习-Leetcode刷题记3:最长回文子串
问题:给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
示例 1:
输入: “babad”
输出: “bab”
注意: “aba” 也是一个有效答案。
示例 2:
输入: “cbbd”
输出: “bb”
解题思路:
待补充
解题过程:
class Solution:
def longestPalindrome(self, s):
max_length = 0
result = ""
for i in range(len(s)):
length, j = 1, 1
while i - j >= 0 and i + j < len(s) and s[i-j] == s[i+j]:
length += 2
j += 1
if length > max_length:
max_length = length
result = s[i-j+1: i+j]
if i > 0 and s[i] == s[i-1]:
length, j = 2, 1
while i - 1 - j >= 0 and i + j < len(s) and s[i-1-j] == s[i+j]:
length += 2
j += 1
if length > max_length:
max_length = length
result = s[i-j: i+j]
return result