题解 | #最长回文子串#

最长回文子串

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

import sys

for s in sys.stdin:
    n = len(s)
    res = []
    for i in range(n):#双指针法,遍历寻找所有满足条件的子串,并放入列表res中
        for j in range(i+1, n+1):
            if s[i:j] == s[i:j][::-1]:#判断当前子串是否为回文串
                res.append(s[i:j])
    
    if res != []:#如果列表res不为空,则输出列表res中最长元素的长度,用到了map()函数
        print(max(map(len, res)))
    else:
        print(1)

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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