当前位置:网站首页>[question brushing diary] longest palindrome substring
[question brushing diary] longest palindrome substring
2022-07-20 15:18:00 【lyz_ fish】
Sum up the questions :
The title is :https://leetcode.cn/problems/longest-palindromic-substring/
MANCHE thought : Insert #
As isolation .
First , See the title , I think we can use the central diffusion method . However, we need to discuss whether odd numbers or even numbers . for example aa
and aba
It's a little troublesome . If you deal with it , Insert #
, Then the palindrome number traversing any character can be calculated as an odd number . Because the original same characters are separated .
#manche Algorithm
s_ = "#"+"#".join(s)+ "#"
ans = 0
for index,value in enumerate(s_):
# Odd number :
left,right,n = index,index,len(s_)
while True:
left -= 1
right += 1
if left>=0 and right < n:# The boundary conditions . Be similar to C++ Medium ++left perhaps ++right.
pass
else:
left += 1# Add back if you reduce more . Step back .
right -= 1
break
if s_[left]==s_[right]:# If the left and right strings are the same , Continue to cycle .
pass
else:
left += 1# Traverse to unequal boundaries , Step back and jump out of the loop .
right -= 1
break
# print("ans\t",s_[left:right+1])
if ans < (len(s_[left:right+1])-1)//2:
res = s_[left:right+1]
ans = (len(s_[left:right+1])-1)//2
return ''.join(res.split('#'))
边栏推荐
- Eolink 和 JMeter 接口测试对比
- 省选专练之棋盘问题
- 使用mindspore创建数据集时报错
- opencv学习(5)之鼠标操作 归一化 放缩 图像翻转
- Devops has been practiced for many years. What is the most painful thing?
- [Golang数据库专题5]Golang语言操作Redis进行增删改查
- Is there a function similar to ifnull in flinksql?
- pytorch训练模型时dataloader报错“default_collate: batch must contain tensors, numpy arrays, numbers, dicts ”
- Uncover the data enhancement potential of MAE, and Shanghai Communications & Huawei proposes masking reconstruction data enhancement based on MAE
- 2022.7.10-----leetcode. seven hundred and forty-one
猜你喜欢
随机推荐
MindSpore serving是否支持热加载,避免推理服务中断
[bzoj2393] cirno's perfect math classroom
01矩阵
Gearman 任务调度程序「建议收藏」
Provincial selected and specially trained [haoi2009] caterpillar
Chessboard problem of provincial selection and professional training
pytorch训练模型时dataloader报错“default_collate: batch must contain tensors, numpy arrays, numbers, dicts ”
Literary and artistic calculation Ji of provincial election and professional training
省选专练之 [HAOI2009]毛毛虫
Leetcode | sliding window
反射、、、
使用mindspore创建数据集时报错
面试官必问的 3 道 MQ 面试题,还有谁不会??
Three moves from 20s to 500ms
Is machine translation finished? Meta open source nllb translation model supports the translation of 200 languages
mos管的输入输出特性曲线及gm/id仿真曲线(cadence IC617)
机器翻译做到头了?Meta开源NLLB翻译模型,支持200种语言互译
APICloud AVM框架打造数字滚动组件
[Golang数据库专题5]Golang语言操作Redis进行增删改查
Zhiyuan admitted plagiarizing the paper, and the relevant responsible person has resigned!