其他分享
首页 > 其他分享> > 求最长的回文子串

求最长的回文子串

作者:互联网

动态规划法

 

def longestPalindrome(s):
    k = len(s)
    matrix = [[0 for i in range(k)] for i in range(k)]
    logestSubStr = ""
    logestLen = 0
    for j in range(0, k):
        for i in range(0, j+1):
            if j - i <= 1:
                if s[i]==s[j]:
                    matrix[i][j] = 1
                    if logestLen < j - i + 1:
                        logestLen = j - i + 1
                        logestSubStr = s[i:j+1]
            else:
                if s[i] == s[j] and matrix[i+1][j-1]:
                    matrix[i][j]=1
                    if logestLen < j - i + 1:
                        logestSubStr =s[i:j+1]
                        logestLen = j - i+ 1
    return logestSubStr

标签:子串,matrix,longestPalindrome,logestSubStr,logestLen,规划法,range,最长,回文
来源: https://blog.csdn.net/lrr_csdn/article/details/87890368