在写leetcode的时候,发现有时候时间相差一点点,排名就有巨大变化
68ms的战胜3.22%的人,40ms的就战胜99.77%的人,代码分别是
class Solution:
def convertToTitle(self, n: 'int') -> 'str':
conv='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
s=""
while True:
n-=1
s+=conv[n%26]
n//=26
if n==0:
break
return ("".join(list(reversed(s))))
class Solution:
def convertToTitle(self, n: 'int') -> 'str':
conv='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
s=[]
while True:
n-=1
s.insert(0,conv[n%26])
n//=26
if n==0:
break
return ("".join(s))
谁知道第一种和第二种相比,哪个步骤占用了比较多的时间?是字符串的拷贝还是reversed()函数还是list()函数