美文网首页
Factorial Trailing Zeroes阶乘尾零

Factorial Trailing Zeroes阶乘尾零

作者: 穿越那片海 | 来源:发表于2017-03-14 22:53 被阅读0次

Easy

给定整数n, 返回n!的尾零的数目。复杂度控制在O(logN)。

n!的尾零主要有5*2造成,因子为2的数很多,所以只需要知道从1到n中5的因子个数即可。注意25有两个因子5,125有三个因子5,。。。,所以需要补上这些多出来的因子。

class Solution(object):
    def trailingZeroes(self, n):
        """
        :type n: int
        :rtype: int
        """
        return 0 if n == 0 else n / 5 + self.trailingZeroes(n / 5)

相关文章

网友评论

      本文标题:Factorial Trailing Zeroes阶乘尾零

      本文链接:https://www.haomeiwen.com/subject/emsknttx.html