当前位置:网站首页>leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
2022-07-06 18:37:00 【白速龙王的回眸】
分析
dp[i][j]表示i到j是否回文
然后特例是dp[i][i] dp[i][i + 1]
其他的dp[i][j] 看 dp[i + 1][j - 1]的值 是否非0
以及s[i] 和 s[j]是否相等
ac code
class Solution:
def longestPalindrome(self, s: str) -> str:
# n * n
# dp[i][j]表示[i, j]的回文长度
n = len(s)
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = 1
maxn = 1
ans = s[0]
for l in range(2, n + 1):
for start in range(n - l + 1):
#print(start, start + l - 1)
if l == 2:
if s[start] == s[start + 1]:
dp[start][start + 1] = 2
else:
dp[start][start + 1] = 0
else:
if s[start] == s[start + l - 1] and dp[start + 1][start + l - 2] > 0:
dp[start][start + l - 1] = dp[start + 1][start + l - 2] + 2
else:
dp[start][start + l - 1] = 0
if dp[start][start + l - 1] > maxn:
maxn = dp[start][start + l - 1]
ans = s[start: start + l]
#print(dp)
return ans
总结
二维dp
后续 反向贪心暴力反而更快。。。
class Solution:
def longestPalindrome(self, s: str) -> str:
def check(s):
return s == s[::-1]
# n * n
# dp[i][j]表示[i, j]的回文长度
n = len(s)
maxn = 1
ans = s[0]
for l in range(n, 0, -1):
for start in range(n - l + 1):
if check(s[start: start + l]) and l > maxn:
maxn = l
ans = s[start: start + l]
return ans
#print(dp)
#return ans
return s[0]
边栏推荐
- 猫猫回收站
- Lumion 11.0软件安装包下载及安装教程
- FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
- NPM install compilation times "cannot read properties of null (reading 'pickalgorithm')“
- ROS learning (26) dynamic parameter configuration
- 【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
- ROS learning (22) TF transformation
- CISP-PTE之命令注入篇
- Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
猜你喜欢
Yiwen takes you into [memory leak]
go swagger使用
ROS learning (26) dynamic parameter configuration
New generation cloud native message queue (I)
ROS学习(21)机器人SLAM功能包——orbslam的安装与测试
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
The mega version model of dall-e MINI has been released and is open for download
Flir Blackfly S 工业相机 介绍
ROS学习(22)TF变换
Blackfly S USB3工业相机:缓冲区处理
随机推荐
开发中对集合里面的数据根据属性进行合并数量时犯的错误
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
猫猫回收站
处理streamlit库上传的图片文件
一片叶子两三万?植物消费爆火背后的“阳谋”
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
CISP-PTE实操练习讲解(二)
Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
使用Ceres进行slam必须要弄清楚的几个类和函数
投资的再思考
Command injection of cisp-pte
JVM memory model
Stm32f4 --- general timer update interrupt
FLIR blackfly s usb3 industrial camera: white balance setting method
Shell script quickly counts the number of lines of project code
centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
Web开发小妙招:巧用ThreadLocal规避层层传值
Draco - glTF模型压缩利器
Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄
String to date object