leetcode第五题
按照奇偶顺序,找到最长的list即可。
代码逻辑非常简单
def longp(s): res = '' for i in range(len(s)): #先判定奇数的,从i开始左右对比 tmp = helper(s,i,i) if len(tmp) > len(res):res = tmp #再判定偶数的,从i和i+1开始对比 tmp = helper(s,i,i+1) if len(tmp) > len(res):res = tmp print(len(res)) def helper(s,l,r): while l >= 0 and r < len(s) and s[l] == s[r]: l -= 1 r += 1 return s[l+1:r] while True: try: s = input() longp(s) except: break