当前位置:网站首页>Force KouTi (5), the longest text string back
Force KouTi (5), the longest text string back
2022-08-04 19:33:00 【metaphor of the world】
最长回文子串
题目内容
给你一个字符串 s,找到 s 中最长的回文子串.
示例 1:
输入:s = "babad"
输出:"bab"
解释:"aba" 同样是符合题意的答案.
示例 2:
输入:s = "cbbd"
输出:"bb"
提示:
1 <= s.length <= 1000
s 仅由数字和英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-palindromic-substring
题解一:中心扩展法
构建一个【Spread from the center to both sides】的函数——The conditions for diffusion are,The elements are equal on both sides of the boundary and spread,函数返回【回文字符串】.
Iterates over each position of the given string,And judge the length of the found palindrome string,返回最长的字符串.
需要注意的是,If the palindrome string length is odd,Then the characters in the middle of the string will not have palindrome characters,比如“bab”,a 只出现一次,But the string is still a palindrome,The opposite is true for even lengths,如“baab”.
class Solution:
def longestPalindrome(self, s: str) -> str:
res = ""
for i in range(0, len(s)):
a = self.findK(s, i, i) # Finds a palindrome string of odd length
b = self.findK(s, i, i+1) # looking for length isoushu 的回文字符串
# Determines the length of the palindrome string
if len(b) > len(a):
if len(b) > len(res):
res = b
else:
if len(a) > len(res):
res = a
return res
def findK(self, s, left, right):
# 寻找回文字符串
ans = ""
while left >= 0 and right <= len(s) - 1 and s[left] == s[right]:
if left == right: # When looking for odd palindrome strings,The first character to be added,并且因为 The left pointer is equal to the right pointer,So just add it again
ans = s[left]
else:
ans = s[left] + ans + s[right]
left -= 1
right += 1
return ans
The time for the force buckle to pass is912 ms
题解二:动态规划
The text of the problem solution uses the force deduction problem to solve the author_Breiman的题解
定义状态:题目让我们求什么,就把什么设置为状态
题目求s中最长的回文子串,那就判断所有子串是否为回文子串,选出最长的
因此:dp[i][j]表示s[i:j+1]是否为回文子串(这里+1是为了构造闭区间)状态转移方程:对空间进行分类讨论(当前ij状态、过去ij状态 如何联合得到输出)
当前ij状态:头尾必须相等(s[i]==s[j])
过去ij状态:去掉头尾之后还是一个回文(dp[i+1][j-1] is True)
边界条件:只要是找过去ij状态的时候,就会涉及边界条件(即超出边界情况处理)
当i==j时一定是回文
j-1-(i+1)<=0,即j-i<=2时,只要当s[i]==s[j]时就是回文,不用判断dp[i+1][j-1]
dp[i][j] 为截取的子串输出内容:每次发现新回文都比较一下长度,记录i与长度
class Solution:
def longestPalindrome(self, s: str) -> str:
max_length = 0
res = ""
length = len(s)
# n * n 的矩阵,元素全是 False
dp = [[False] * length for i in range(length)]
if length == 1:
return s
for right in range(length):
for left in range(right+1):
index = right - left + 1
if index == 1:
dp[left][right] = True
elif index == 2:
dp[left][right] = (s[left] == s[right])
else:
dp[left][right] = (s[left] == s[right] and dp[left+1][right-1])
if dp[left][right]:
if max_length < index:
max_length = index
res = s[left: right+1]
return res
The time for the force buckle to pass is4924 ms
边栏推荐
猜你喜欢

四维图新:子公司首款功能安全 MCU 芯片已陆续送样

华为WLAN技术:AP上线及相关模板的配置实验

Highlights of some performance tests

Defaced Fingerprint Recovery and Identification

Yuanguo chain game system development

直播回顾|7 月 Pulsar 中文开发者与用户组会议

Pedestrian fall detection experiment based on YOLOV5

PG网络传输安全SSL介绍及使用示例

高效目标检测:动态候选较大程度提升检测精度(附论文下载)

前3名突然变了,揭秘 7 月编程语言最新排行榜
随机推荐
Finished product upgrade program
Internship: changed the requirements
电脑一键重装系统内存完整性无法打开怎么办
nr部分计算
数据库治理的探索与实践
The list of Kubernetes - watch mechanism
正畸MIA微种植体支抗技术中国10周年交流会在沈举办
WPF 多个 StylusPlugIn 的事件触发顺序
【Attention演变史】翻译模型seq2seq (第二弹)
How to monitor code cyclomatic complexity by refactoring indicators
c sqlite ... ...
JS: 数组和树的相互转换
用“绿色计算“技术推动算力可持续发展
完善的交叉编译环境记录 peta 生成的shell 脚本
PostgreSQL的 SPI_接口函数
win10 uwp win2d 使用 Path 绘制界面
Finger Vein Recognition-matlab
How to manually download and install SAP Fiori tools - Extension Pack for Visual Studio Code
七夕福利!中奖名单:书籍免费送!
程序员如何在职场上少走弯路?