题目
题目就是对一个数求阶乘,然后判断这个数后面有几个0.
我的代码1:(未通过)
我一开始以为他很简单,于是使用了如下的代码,发现超时:
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
x=math.factorial(n)
count=0
while x>0:
if x%10==0:
count+=1
else:
break
x/=10
return count
太巧妙了!!
代码
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 0
else:
return n//5 + self.trailingZeroes(n//5)