leetcode 172 Factorial Trailing Zeroes

简介: Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.解决思路: 决定阶乘末尾零的个数其实是数列中5出现的次数,比如5的阶乘一个零。

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

解决思路:
决定阶乘末尾零的个数其实是数列中5出现的次数,比如5的阶乘一个零。1024的阶乘末尾到底有几个零呢?

http://bbs.csdn.net/topics/380161955

这里写图片描述
代码如下:

int trailingZeroes(int n) 
{
    int total = 0;

    while(n>=5)
    {
        n = (n-(n%5))/5;
        total = total + n;
    }

    return total;

}

python 的解决方案:

class Solution:
    # @return an integer
    def trailingZeroes(self, n):
        factor, count = 5, 0

        while True:
            curCount = n // factor
            if not curCount:
                break

            count += curCount
            factor *= 5

        return count
相关文章
LeetCode 172 Factorial Trailing Zeroes(阶乘后的零)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50568854 翻译 给定一个整型n,返回n!后面的零的个数。
596 0
|
26天前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
1月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
1月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3
|
1月前
|
容器
《LeetCode》——LeetCode刷题日记1
《LeetCode》——LeetCode刷题日记1
|
1月前
|
算法
LeetCode刷题---21.合并两个有序链表(双指针)
LeetCode刷题---21.合并两个有序链表(双指针)
|
1月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
1月前
|
算法 测试技术
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
|
1月前
|
存储
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
143 38
|
1天前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
6 0

热门文章

最新文章