当前位置:网站首页>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]
边栏推荐
- Stm32f4 --- PWM output
- How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
- Freeswitch dials extension number source code tracking
- freeswitch拨打分机号源代码跟踪
- Seconds understand the delay and timing function of wechat applet
- Redis tool class redisutil (tool class III)
- New generation cloud native message queue (I)
- postgresql之整體查詢大致過程
- Introduction to FLIR blackfly s industrial camera
- Date processing tool class dateutils (tool class 1)
猜你喜欢
[paper reading | deep reading] dngr:deep neural networks for learning graph representations
企业中台建设新路径——低代码平台
fiddler的使用
Cat recycling bin
Web3的先锋兵:虚拟人
Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
[unity notes] screen coordinates to ugui coordinates
一片叶子两三万?植物消费爆火背后的“阳谋”
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
#夏日挑战赛#数据库学霸笔记(下)~
随机推荐
Stm32f4 --- general timer update interrupt
本周 火火火火 的开源项目!
1个月增长900w+播放!总结B站顶流恰饭的2个新趋势
postgresql之整体查询大致过程
MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
一片叶子两三万?植物消费爆火背后的“阳谋”
How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
STM32项目 -- 选题分享(部分)
Word wrap when flex exceeds width
传感器:土壤湿度传感器(XH-M214)介绍及stm32驱动代码
Web3的先锋兵:虚拟人
Use of pgpool II and pgpooladmin
Robot team learning method to achieve 8.8 times human return
Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!
Freeswitch dials extension number source code tracking
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Data connection mode in low code platform (Part 1)
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
The foreground downloads network pictures without background processing
组合导航:中海达iNAV2产品描述及接口描述