给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
题目: https://leetcode-cn.com/problems/longest-palindromic-substring/submissions/
写的不好,不容宜写剪枝。太耗时了。
class Solution:
def longestPalindrome(self, s: str) -> str:
index = [[0 for i in range(1005)] for j in range(1005)]
for i in range(len(s)):
index[i][i] = 1
begin, end = 0, 0
not_exist = []
for i in range(2,len(s)+1):
jump = False
flag = False
for each in not_exist:
if (i-each)%2 == 0:
jump=True
break
if jump:
continue
for j in range(0,len(s)-i+1):
if s[j]==s[j+i-1] and (index[j+1][j+i-2]==1 or i==2):
index[j][j+i-1] = 1
begin = j
end = j+i-1
flag = True
if not flag:
not_exist.append(i)
return s[begin:end+1]
原文:https://www.cnblogs.com/Lzqayx/p/12126492.html