当前位置:网站首页>【LeetCode】5. Longest Palindromic Substring
【LeetCode】5. Longest Palindromic Substring
2022-06-12 22:22:00 【LawsonAbs】
1 subject
This question is similar to one , It is also related to palindromes .
2 thought
dp Related questions
dp[i,j]Expresss[i,j]Is it a palindrome string- On the basis of the above, we only need to judge
dp[i+1,j+1]Whether it is a palindrome string , So we have to recurse . - Finally double for Loop through the longest string
3 Code
class Solution:
def longestPalindrome(self, s: str) -> str:
dp = [[0]*len(s) for i in range(len(s))]
n = len(s)
for i in range(n):
dp[i][i] = 1 # Itself is a palindrome string
for span in range(2,n+1): # Interval length from 2 Start
for left in range(0,n-span+1):
right = left+span -1
if s[left] == s[right]: # If two characters are equal
if left +1 == right:
dp[left][right] = 1
elif dp[left+1][right-1] :
dp[left][right] = 1
res = s[0]
max_len = 1
for i in range(n):
for j in range(i+1,n):
if dp[i][j] == 1:
if j-i+1 > max_len:
res = s[i:j+1]
max_len = j-i+1
# print(dp)
return res
边栏推荐
- [machine learning] learning notes 01- introduction
- Leetcode Yanghui triangle
- How to specify your webpage's language so Google Chrome doesn't offer to translate it
- Ansible roles project case (IV)
- vim利用右下4键
- Mysql concat_ WS, concat function use
- talent showing itself! Oceanbase was selected into the 2021 "sci tech innovation China" open source innovation list
- Kotlin collaboration process - flow
- JVM foundation - > what is STW?
- Logstash timestamp converted to UNIX nanosecond nano second time
猜你喜欢

PCB package download website recommendation and detailed usage

Audio and video technology development weekly 𞓜 234

Kotlin collaboration process - flow

数据库每日一题---第10天:组合两个表

年薪50万是一条线,年薪100万又是一条线…...

Things about the kotlin collaboration process - pipeline channel

Ansible foundation and common modules (I)

Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases

JVM foundation > G1 garbage collector

Photoshop:PS如何实现放大图片不模糊
随机推荐
MySQL introduction and installation (I)
Jin AI her power | impact tech, she can
vim利用右下4键
QT quick 3D learning: use mouse and keyboard to control node position and direction
四元数简介
3.5 setup and teardown of test classes
Ansible foundation and common modules (I)
LNMP platform docking redis service
Unity commonly used 3D mathematical calculation
About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started
Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
Prefix sum and difference
证券开户有风险吗?怎么开户安全呢?
微信小程序提现功能
动态规划之如何将问题抽象转化为0-1背包问题(详解利用动态规划求方案数)
Research Report on market supply and demand and strategy of tizanidine industry in China
Ansible playbook and variable (II)
【LeetCode】102. 二叉树的层序遍历
最近公共祖先问题你真的学会了吗?
【LeetCode】103. 二叉树的锯齿形层序遍历