当前位置:网站首页>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]
边栏推荐
- Sensor: DS1302 clock chip and driver code
- Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
- String to date object
- 投资的再思考
- FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
- Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
- LeetCode. Sword finger offer 62 The last remaining number in the circle
- Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
- 【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
- Modify the system time of Px4 flight control
猜你喜欢

ROS learning (XX) robot slam function package -- installation and testing of rgbdslam

Correct use of BigDecimal

Blackfly s usb3 industrial camera: buffer processing

New generation cloud native message queue (I)

Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption

建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!

String or binary data will be truncated

centos8 用yum 安装MySQL 8.0.x

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效

ROS learning (22) TF transformation
随机推荐
Flir Blackfly S USB3 工业相机:白平衡设置方法
Dall-E Mini的Mega版本模型发布,已开放下载
Analyze "C language" [advanced] paid knowledge [i]
2022 system integration project management engineer examination knowledge point: Mobile Internet
Lumion 11.0软件安装包下载及安装教程
Shortcut keys commonly used in idea
[unique] what is the [chain storage structure]?
centos8安装mysql报错:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
BigDecimal 的正确使用方式
Date processing tool class dateutils (tool class 1)
npm install 编译时报“Cannot read properties of null (reading ‘pickAlgorithm‘)“
激光雷达:Ouster OS产品介绍及使用方法
组合导航:中海达iNAV2产品描述及接口描述
RC振荡器和晶体振荡器简介
Processing image files uploaded by streamlit Library
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!
Analyze "C language" [advanced] paid knowledge [End]
Recognition of C language array
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient