#計算一個字串中最長的 回文子串
s="abcicbads"
def create_echo(s):
temp,max_p,length="","",len(s)
for i in range(length):
left1,right1=i,i
def compare(l,r):
while l != -1 and r != length and s[l]==s[r]:
l,r=l-1,r+1
if l == -1 or r == length:
return s[l+1:r]
temp=compare(left1,right1)
if len(temp)>len(max_p): #此行報錯TypeError: 'NoneType' object is not iterable
max_p=temp
if s[i]==s[i+1]:
left2,right2=i,i+1
temp=compare(left2,right2)
if len(temp)>len(max_p):
max_p=temp
return max_p
print(create_echo(s))
求各位大神解答
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/39870.html
上一篇:下面這行代碼寫得似乎很高級,我這初學者沒看懂,誰給解釋一下!
下一篇:c語言基礎題
