当前位置:网站首页>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]
边栏推荐
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- Correct use of BigDecimal
- Recommended collection!! Which is the best flutter status management plug-in? Please look at the ranking list of yard farmers on the island!
- Treadpoolconfig thread pool configuration in real projects
- 阿里云中间件开源往事
- Lumion 11.0软件安装包下载及安装教程
- Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
- 激光雷达:Ouster OS产品介绍及使用方法
- Cisp-pte practice explanation (II)
- 一片葉子兩三萬?植物消費爆火背後的“陽謀”
猜你喜欢

Blackfly S USB3工业相机:缓冲区处理

Data connection mode in low code platform (Part 1)

#夏日挑战赛#数据库学霸笔记(下)~

UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢

leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】

Lombok同时使⽤@Data和@Builder 的坑
![[unity notes] screen coordinates to ugui coordinates](/img/e4/fc18dd9b4b0e36ec3e278e5fb3fd23.jpg)
[unity notes] screen coordinates to ugui coordinates

go swagger使用

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!

老板被隔离了
随机推荐
企业中台建设新路径——低代码平台
最近小程序开发记录
Metaforce force meta universe development and construction - fossage 2.0 system development
STM32F4---PWM输出
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
The cities research center of New York University recruits master of science and postdoctoral students
Centos8 install MySQL 8.0 using yum x
XML to map tool class xmlmaputils (tool class V)
Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Correct use of BigDecimal
Processing image files uploaded by streamlit Library
FLIR blackfly s usb3 industrial camera: white balance setting method
Time synchronization of livox lidar hardware -- PPS method
阿里云中间件开源往事
Several classes and functions that must be clarified when using Ceres to slam
Lidar: introduction and usage of ouster OS
Command injection of cisp-pte
STM32F4---通用定时器更新中断
argo workflows源码解析