Well, to compute the number of trailing zeros, we need to first think clear about what will generate a trailing 0? Obviously, a number multiplied by 10 will have a trailing 0 added to it. So we only need to find out how many 10's will appear in the expression of the factorial. Since 10 = 2 * 5and there are a bunch more 2's (each even number will contribute at least one 2), we only need to count the number of 5's.

Now let's see what numbers will contribute a 5. Well, simply the multiples of 5, like 5, 10, 15, 20, 25, 35, .... So is the result simply n / 5? Well, not that easy. Notice that some numbers may contribute more than one 5, like 25 = 5 * 5. Well, what numbers will contribute more than one 5? Ok, you may notice that only multiples of the power of 5 will contribute more than one 5. For example, multiples of 25 will contribute at least two 5's.

Well, how to count them all? If you try some examples, you may finally get the result, which is n / 5 + n / 25 + n / 125 + .... The idea behind this expression is: all the multiples of 5 will contribute one 5, the multiples of 25 will contribute one more 5 and the multiples of 125 will contribute another one more 5... and so on. Now, we can write down the following code, which is pretty short.

 class Solution {
public:
int trailingZeroes(int n) {
int count = ;
for (long long i = ; n / i; i *= )
count += n / i;
return count;
}
};

[LeetCode] Factorial Trailing Zeros的更多相关文章

  1. [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

    LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...

  2. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

  3. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  5. 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解

    题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...

  6. [LeetCode] Factorial Trailing Zeroes 阶乘末尾0

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  7. Python3解leetcode Factorial Trailing Zeroes

    问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...

  8. LeetCode Factorial Trailing Zeroes (阶乘后缀零)

    题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...

  9. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. 阻塞IO、非阻塞IO的区别

    1.类与类之间的关系:依赖,实现,泛化(继承),关联,组合,聚合. 1)依赖(虚线):一个类是 另一个类的函数参数 或者 函数返回值. 2)实现(实线加小圆):对纯虚函数类(抽象类)的实现. 3)继承 ...

  2. wx小程序的学习

    传送门: # 微信小程序案例大全 https://www.cnblogs.com/icyhoo/p/6282574.html # 微信小程序开发工具 https://mp.weixin.qq.com/ ...

  3. Laravel之路(事务)mysql事务

    其实关于mysql的事务(原声mysql语句),我在我的博客里面有提到(mysql的文章分类下) 今天看下基于laravel框架ORM的处理 准备: 表必须是InnoDB引擎 DB::beginTra ...

  4. 分享分享JavaScript100例-仅供参考

    最近一直在做项目,分享下以前收集的Javascript100例,仅供参考. http://files.cnblogs.com/52net/JavaScript100例.zip

  5. uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)

    题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...

  6. PHP学习笔记(16)AJAX无刷新技术--深入理解

    Ajax里的onreadystatechange的作用是什么 发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态,XMLHttpRequest对象提供了onrea ...

  7. BZOJ2820 YY的GCD 莫比乌斯+系数前缀和

    /** 题目:BZOJ2820 YY的GCD 链接:http://www.cogs.pro/cogs/problem/problem.php?pid=2165 题意:神犇YY虐完数论后给傻×kAc出了 ...

  8. JavaScript越来越简单啦啦啦

    我正在对需要从远程API提取并对页面的各个部分进行更改的页面进行更改.听起来像是抽出jQuery和Ajax的时候了,不是吗?相反,我只是使用了老式的JavaScript.实际上,我使用了新的JavaS ...

  9. .NET平台下 极光推送

    正好看到别人发了个极光的推送例子,想来前面也刚做过这个,就把我的push类共享下 public class JPush { /// <summary> /// push信息到手机应用上 J ...

  10. python通过日志分析加入黑名单

    监控nginx日志,若有人攻击,则加入黑名单,操作步骤如下:1.读取日志文件2.分隔文件,取出ip3.将取出的ip放入list,然后判读ip的次数4.若超过设定的次数,则加入黑名单 日志信息如下: 1 ...