当前位置:网站首页>【LeetCode】5. 最长回文子串
【LeetCode】5. 最长回文子串
2022-06-12 22:17:00 【LawsonAbs】
1 题目
这题和有一题好像,也是回文子串相关题。
2 思想
dp相关题
dp[i,j]表示s[i,j]是否是回文串- 在上述基础上我们只需要判断
dp[i+1,j+1]是否是回文串即可,于是便得递推式。 - 最后再双重for循环找一下最长的串
3 代码
class Solution:
def longestPalindrome(self, s: str) -> str:
dp = [[0]*len(s) for i in range(len(s))]
n = len(s)
for i in range(n):
dp[i][i] = 1 # 自身是回文串
for span in range(2,n+1): # 区间长度从2开始
for left in range(0,n-span+1):
right = left+span -1
if s[left] == s[right]: # 如果两个字符相等
if left +1 == right:
dp[left][right] = 1
elif dp[left+1][right-1] :
dp[left][right] = 1
res = s[0]
max_len = 1
for i in range(n):
for j in range(i+1,n):
if dp[i][j] == 1:
if j-i+1 > max_len:
res = s[i:j+1]
max_len = j-i+1
# print(dp)
return res
边栏推荐
- Unity 常用3D数学计算
- SQL tuning guide notes 12:configuring options for optimizer statistics gathering
- 孙老师版本JDBC(2022年6月12日21:34:25)
- Prefix sum and difference
- SQL tuning guide notes 17:importing and exporting optimizer statistics
- JVM foundation - what is the process of loading > objects into the JVM, and then clearing them by GC?
- Ansible Roles-项目案例(四)
- USB mechanical keyboard changed to Bluetooth Keyboard
- What is embedded
- IPhone: save Boolean into core data - iphone: save Boolean into core data
猜你喜欢

Ansible-大总结(六)

Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)

The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him

Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)

Qt Quick 3D学习:鼠标拾取物体
![[image denoising] image denoising based on trilateral filter with matlab code](/img/f2/770a0e2938728e731c18c0a66f7c12.png)
[image denoising] image denoising based on trilateral filter with matlab code

数据库每日一题---第10天:组合两个表

【数据分析】基于 kmeans实现数据聚类分组含Matlab源码

Xingda easy control modbustcp to profibusdp

JVisualVM初步使用
随机推荐
【图像去噪】基于三边滤波器实现图像去噪附matlab代码
"Oracle database parallel execution" technical white paper reading notes
How to prevent phishing emails? S/mime certificate to help!
Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
A puzzle about + =
Cloning PDB with ADG standby
How to ensure thread safety?
SQL query list all views in SQL Server 2005 database - SQL query to list all views in an SQL Server 2005 database
JVM Basics - > What are the JVM parameters?
#yyds干货盘点# 解决剑指offer:字符流中第一个不重复的字符
Qt Quick 3D学习:使用鼠标键盘控制节点位置和方向
June training (day 10) - bit operation
logstash时间戳转换为unix 纳秒nano second time
建立高可用的数据库
MySQL体系结构及基础管理(二)
[QNX hypervisor 2.2 user manual] 4.3 obtain the host component
【概率论与数理统计】期末复习抱佛脚:公式总结与简单例题(完结)
Afraid to write documents? AI plug-in for automatically generating code documents
USB mechanical keyboard changed to Bluetooth Keyboard
How to abstract a problem into a 0-1 knapsack problem in dynamic programming