本文共 585 字,大约阅读时间需要 1 分钟。
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3Output: 0Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5Output: 1Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
<思路>刚开始想的是数字末位有几个5,结果就有几个0(5*2=10),有几个25,就再多几个0(25*4=100,有两个0,不过在除以5时已经算过一个),以此类推,125再多一个0(125*8=1000),625再多一个0....
class Solution(object): def trailingZeroes(self, n): """ :type n: int :rtype: int """ num=0 while n>0: num += n//5 n /= 5 return num
转载地址:http://virbb.baihongyu.com/