当前位置:网站首页>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]
边栏推荐
- The GPG keys listed for the "MySQL 8.0 community server" repository are already ins
- 最近小程序开发记录
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- 机器人队伍学习方法,实现8.8倍的人力回报
- Shell script quickly counts the number of lines of project code
- 老板被隔离了
- Livox激光雷达硬件时间同步---PPS方法
- Cisp-pte practice explanation (II)
- CISP-PTE之命令注入篇
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
猜你喜欢
Recognition of C language array
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
老板被隔离了
Modify the system time of Px4 flight control
Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
STM32F4---通用定时器更新中断
微服务架构介绍
CISP-PTE之命令注入篇
Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
随机推荐
Ros Learning (23) Action Communication Mechanism
Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
Input and output of C language pointer to two-dimensional array
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
The use of video in the wiper component causes full screen dislocation
FLIR blackfly s usb3 industrial camera: white balance setting method
npm install 编译时报“Cannot read properties of null (reading ‘pickAlgorithm‘)“
机器人队伍学习方法,实现8.8倍的人力回报
Recognition of C language array
Integrated navigation: product description and interface description of zhonghaida inav2
#yyds干货盘点# 解决名企真题:最大差值
Word wrap when flex exceeds width
组合导航:中海达iNAV2产品描述及接口描述
Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
[unique] what is the [chain storage structure]?
New generation cloud native message queue (I)
一片叶子两三万?植物消费爆火背后的“阳谋”
FLIR blackfly s industrial camera: auto exposure configuration and code