题解 | #最长回文子串#

最长回文子串

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

  • 中心拓展法来求解,O(n^2)
string = input()

# 中心拓展法
def expand(s, i, j):
    ans = 0
    while i >=0 and j < len(s) and s[i] == s[j]:
        ans = j - i + 1
        i = i - 1
        j = j + 1
    return ans

ans = 0
for i in range(len(string)):
    ans = max(ans, expand(string, i, i))
    ans = max(ans, expand(string, i, i+1))
print(ans

全部评论

相关推荐

没有offer的呆呆:日常和暑期都投试一试,3月份机会挺多的
点赞 评论 收藏
分享
02-22 21:16
已编辑
门头沟学院 运营
牛客928043833号:离了你谁还拿我当个宝
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务