[LeetCode] Factorial Trailing Zeros

简介: 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.

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.

1 class Solution {
2 public:
3     int trailingZeroes(int n) { 
4         int count = 0;
5         for (long long i = 5; n / i; i *= 5)
6             count += n / i;
7         return count;
8     }
9 };

 

目录
相关文章
[LeetCode]--29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 以前我记得做过乘法变加法吧,这个有点像除法变减法,用位运算,二进制嘛,左移一位相当于乘以二。 一个有趣的是 Math.abs(-2147483648
1133 0
|
算法
[LeetCode]--31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest
1304 0
|
Java
[LeetCode]--371. Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. Credits: Special thanks to @fujiaozhu for addin
1156 0
|
机器学习/深度学习
[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. Credits: Special thanks to @ts for adding this problem and c
1056 0
LeetCode - 29. Divide Two Integers
29. Divide Two Integers Problem's Link  ---------------------------------------------------------------------------- Mean:  不使用乘法、除法、取模运算,实现两个数相除.
833 0
LeetCode - 16. 3Sum Closest
16. 3Sum Closest  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个整数序列和一个目标数,在序列中找出三个数,使得这三个数的和与目标数的差最小.
695 0
LeetCode 172 Factorial Trailing Zeroes(阶乘后的零)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50568854 翻译 给定一个整型n,返回n!后面的零的个数。
614 0