前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode 172 Factorial Trailing Zeroes

leetcode 172 Factorial Trailing Zeroes

作者头像
流川疯
发布2019-01-18 16:04:49
4560
发布2019-01-18 16:04:49
举报
文章被收录于专栏:流川疯编写程序的艺术

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

这里写图片描述
这里写图片描述

代码如下:

代码语言:javascript
复制
int trailingZeroes(int n) 
{
    int total = 0;

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

    return total;

}

python 的解决方案:

代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015年05月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档