5. Longest Palindromic Substring

5. Longest Palindromic Substring

python 复制代码
class Solution:
    def longestPalindrome(self, s: str) -> str:
        if len(s)<=1:return s
        ansl=1
        anss=s[0]

        dp=[[False for i in range(len(s))] for j in range(len(s))]
        for i in range(len(s)):
            dp[i][i]=True
            for j in range(i):
                if s[i]==s[j] and (i-j<=2 or dp[j+1][i-1]):
                    dp[j][i]=True
                    if i+1-j>ansl:
                        ansl=i+1-j
                        anss=s[j:i+1]
        return anss
                


    

枚举起点终点位置

相关推荐
__AtYou__6 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
转调7 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
huanxiangcoco8 小时前
152. 乘积最大子数组
python·leetcode
希望有朝一日能如愿以偿9 小时前
力扣题解(飞机座位分配概率)
算法·leetcode·职场和发展
Espresso Macchiato9 小时前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
数据分析螺丝钉11 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
￴ㅤ￴￴ㅤ9527超级帅11 小时前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
鱼跃鹰飞11 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试
源代码•宸15 小时前
Leetcode—76. 最小覆盖子串【困难】
c++·经验分享·算法·leetcode·滑动窗口
百里守约学编程15 小时前
70. 爬楼梯
算法·leetcode·go