当前位置:网站首页>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]
边栏推荐
- Introduction to RC oscillator and crystal oscillator
- Robot team learning method to achieve 8.8 times human return
- Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
- Halcon knowledge: segment_ contours_ XLD operator
- String to date object
- New generation cloud native message queue (I)
- Integrated navigation: product description and interface description of zhonghaida inav2
- 最近小程序开发记录
- Sensor: DS1302 clock chip and driver code
- Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
猜你喜欢

微服务架构介绍

ROS learning (XIX) robot slam function package cartographer

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

ROS学习(21)机器人SLAM功能包——orbslam的安装与测试

使用Ceres进行slam必须要弄清楚的几个类和函数

将截断字符串或二进制数据

New generation cloud native message queue (I)

ROS learning (26) dynamic parameter configuration

ROS learning (22) TF transformation

Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
随机推荐
#yyds干货盘点# 解决名企真题:最大差值
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!
Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
Alibaba cloud middleware open source past
RC振荡器和晶体振荡器简介
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
Livox激光雷达硬件时间同步---PPS方法
ROS學習(23)action通信機制
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
C language [23] classic interview questions [Part 2]
How can I code for 8 hours without getting tired.
MySQL execution process and sequence
Analyze "C language" [advanced] paid knowledge [II]
Web开发小妙招:巧用ThreadLocal规避层层传值
Cat recycling bin
A new path for enterprise mid Platform Construction -- low code platform
Sensor: DS1302 clock chip and driver code
Cisp-pte practice explanation (II)
String to date object