Day30力扣打卡

打卡记录

Day30力扣打卡_第1张图片


最长回文子序列(区间DP)

链接

class Solution:
    def longestPalindromeSubseq(self, s: str) -> int:
        n = len(s)
        f = [[0] * n for _ in range(n)]
        max = lambda x, y: x if x > y else y
        for i in range(n - 1, -1, -1):
            f[i][i] = 1
            for j in range(i + 1, n):
                if s[i] == s[j]: f[i][j] = f[i + 1][j - 1] + 2
                else: f[i][j] = max(f[i + 1][j], f[i][j - 1])
        return f[0][-1]

你可能感兴趣的:(leetcode刷题打卡,leetcode,python,算法)