leetcode——172.阶乘后的零

超时:

 1 class Solution:
 2     def trailingZeroes(self, n: int) -> int:
 3         if n<5:
 4             return 0
 5         b=n
 6         i=n
 7         while i>1:
 8             i=i-1
 9             b=b*i
10         
11         j=0    
12         t=10
13         while b%t==0:
14             j=j+1
15             t=t*10
16         return j

再次超出时间限制:

 1 class Solution:
 2     def trailingZeroes(self, n: int) -> int:
 3         if n<5:
 4             return 0
 5         b=n
 6         i=n
 7         while i>1:
 8             i=i-1
 9             b=b*i
10         b=str(b)
11 
12         for i in range(len(b)-1,-1,-1):
13             if int(b[i])>0:
14                 return len(b)-1-i

通过:

数因子里面5的个数:

1 class Solution:
2     def trailingZeroes(self, n: int) -> int:
3         if n<5:
4             return 0
5         count=0
6         while n>=5:
7             count=count+n//5
8             n=n//5
9         return count
执行用时 :72 ms, 在所有 Python3 提交中击败了17.11%的用户
内存消耗 :13.7 MB, 在所有 Python3 提交中击败了5.19%的用户

                                                                                          ——2019.9.24

我的前方是万里征途,星辰大海!!
原文地址:https://www.cnblogs.com/taoyuxin/p/11578488.html