题解 | 中心拓展法#最长回文子串#

最长回文子串

http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507

与密码截取这道题一模一样

def spread(left, right):
    ans=0
    while left>=0 and right<= n-1 and s[left]==s[right]:
        ans =right - left +1
        left-=1
        right+=1
    return ans

while True:
    try:
        s = input()
        n = len(s)
        max_ans = 0
        for i in range(n):
            ans1 = spread(i,i)
            ans2 = spread(i,i+1)
            max_ans = max(ans1,ans2,max_ans)
        print(max_ans)
    except:
        break
            
        
    
全部评论

相关推荐

06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
12
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务