博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode/LintCode] Factorial Trailing Zeros
阅读量:6935 次
发布时间:2019-06-27

本文共 727 字,大约阅读时间需要 2 分钟。

Problem

Write an algorithm which computes the number of trailing zeros in n factorial.

Challenge

11! = 39916800, so the output should be 2

Note

i是5的倍数,先找有多少个5(1个0),然后找多少个25(2个0),补上,然后多少个125(3个0),补上……

Solution

class Solution {    public long trailingZeros(long n) {        long i = 5;        long count = 0;        while (i <= n) {         //n = 125, i = 5, count = 25; 25个5         //i = 25, count += 5(5个25)= 30; i = (1个)125, count  += 1 = 31;             count += n / i;            i = i * 5;        }        return count;    }}

LeetCode version

class Solution {    public int trailingZeroes(int n) {        int count = 0;        while (n > 0) {            count += n/5;            n /= 5;        }        return count;    }}

转载地址:http://oibnl.baihongyu.com/

你可能感兴趣的文章
超级课程表API
查看>>
机器学习在启动耗时测试中的应用及模型调优(一)
查看>>
LeetCode 62.Unique Paths
查看>>
从AI医疗到量子计算,亚洲研究院如何成为微软发展的生命力?
查看>>
线程冲突 - Thread interference
查看>>
《图解HTTP》读书笔记
查看>>
vuejs实现一个简单的日期组件
查看>>
[单刷APUE系列]第十一章——线程[2]
查看>>
【CTO讲堂】揭秘高效协作工具背后的技术架构
查看>>
【答疑】对象存储OSS常见问题解答(工具类1)
查看>>
ASP.NET Core使用Jaeger实现分布式追踪
查看>>
RDS数据订阅服务使用说明
查看>>
如何优雅拒绝产品经理的不合理需求
查看>>
快轮刘峰:用天才发明解决城市最后5公里出行问题
查看>>
微服务架构 - 巧妙获取被墙的Docker镜像
查看>>
创新型服务“场景串接”——互联网平台建设
查看>>
鲸仓科技获7500万元B+轮融资,旷视科技领投、百度风投跟投
查看>>
IT专家们谈OpenStack和Kubernetes的未来
查看>>
jQuery Ajax 操作函数及deferred对象
查看>>
Eclipse 开发配置
查看>>