题目链接在这里:7. 整数反转 - 力扣(LeetCode)
这道题学习了list类型不能在没有定义长度的情况下直接访问里面的第i个元素,应该使用append或者在开始的时候就a = [0 for _ in range(n)]
1 class solution: 2 def longest(self,s : str)->str: 3 ans = 0 4 lft = rgt = 0 5 n = len(s) 6 f=[[False] * n for _ in range(n)] 7 ans = 1 8 for i in range(n): 9 f[i][i] = True 10 for i in range(n-1): 11 if s[i] == s[i+1] : 12 f[i][i+1] = True 13 ans = 2 14 lft = i 15 else: 16 f[i][i+1] = False 17 for length in range(3, n+1): 18 for i in range(n-length+1): 19 j = i+length - 1 20 # print(i,j) 21 if s[i] == s[j]: 22 f[i][j]=f[i+1][j-1] 23 if f[i][j]==True: 24 ans = length 25 lft = i 26 return s[lft: lft+ans] 27 28 if __name__=="__main__": 29 s = "ccc" 30 an = solution.longest(self=0,s=s) 31 print(an)
标签:__,反转,length,整数,lft,range,ans,True,Leetcode From: https://www.cnblogs.com/keximeiruguo/p/17238334.html