一、leetcode地址
https://leetcode.com/problems/longest-palindromic-substring/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def longestPalindrome(self, s: str) -> str:
len_ = len(s)
if len_ < 2:return s
dp = [[0 for _ in range(len_)] for _ in range(len_)]
max_len = 1;res = s[0]
for i in range(1,len_):
for j in range(i):
if s[i]==s[j] and (j>=i-2 or dp[i-1][j+1]):
dp[i][j] = 1
if i-j+1 > max_len and dp[i][j]:
max_len = i-j+1
res = s[j:i+1]
return res