当前位置:网站首页>leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
2022-07-07 02:24:00 【White speed Dragon King's review】
analysis
dp[i][j] Express i To j Whether palindrome
And the special case is dp[i][i] dp[i][i + 1]
Other dp[i][j] see dp[i + 1][j - 1] Value Whether it's not 0
as well as s[i] and s[j] Whether it is equal or not
ac code
class Solution:
def longestPalindrome(self, s: str) -> str:
# n * n
# dp[i][j] Express [i, j] Palindrome length of
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
summary
A two-dimensional dp
follow-up Reverse greedy violence is faster ...
class Solution:
def longestPalindrome(self, s: str) -> str:
def check(s):
return s == s[::-1]
# n * n
# dp[i][j] Express [i, j] Palindrome length of
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]
边栏推荐
- 4--新唐nuc980 挂载initramfs nfs文件系统
- Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
- 处理streamlit库上传的图片文件
- Flir Blackfly S 工业相机:自动曝光配置及代码
- Sensor: DS1302 clock chip and driver code
- 真实项目,用微信小程序开门编码实现(完结)
- Seconds understand the delay and timing function of wechat applet
- Introduction to FLIR blackfly s industrial camera
- 3D激光SLAM:Livox激光雷达硬件时间同步
- A new path for enterprise mid Platform Construction -- low code platform
猜你喜欢
张平安:加快云上数字创新,共建产业智慧生态
Stm32f4 --- general timer update interrupt
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
postgresql之整体查询大致过程
阿里云中间件开源往事
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
The mega version model of dall-e MINI has been released and is open for download
一片叶子两三万?植物消费爆火背后的“阳谋”
Centos8 install MySQL 8.0 using yum x
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
随机推荐
Overall query process of PostgreSQL
The mega version model of dall-e MINI has been released and is open for download
Web开发小妙招:巧用ThreadLocal规避层层传值
Cat recycling bin
Seconds understand the delay and timing function of wechat applet
Web3对法律的需求
云原生混部最后一道防线:节点水位线设计
Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
1个月增长900w+播放!总结B站顶流恰饭的2个新趋势
阿里云中间件开源往事
Yyds dry goods inventory # solve the real problem of famous enterprises: maximum difference
[paper reading | deep reading] dngr:deep neural networks for learning graph representations
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
4--新唐nuc980 挂载initramfs nfs文件系统
Treadpoolconfig thread pool configuration in real projects
MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
A new path for enterprise mid Platform Construction -- low code platform
Big guys gather | nextarch foundation cloud development meetup is coming!