[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. 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 };
时间: 2024-10-10 10:02:40

[LeetCode] Factorial Trailing Zeros的相关文章

[LeetCode] Factorial Trailing Zeroe

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解题思路: n!=2^x*3^y*5^z...,注意到一个2和一个5贡献一个末尾的0,因此只需计算min(x,z)即可.又因为n/2和n/5分别表示n!中的因子能被2和5整除的个数,n/2>=n/

[LeetCode]65. Factorial Trailing Zeros阶乘的尾零个数

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 creating all test cases. Subscribe to see which companies asked th

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

题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0的个数(如5!=120,则后缀中0的个数为1). 解题思路: 1 int trailingZeroes(int n) { 2 return (n/5>0)?trailingZeroes(n/5)+n/5:0; 3 } 首先这是LeetCode中时间复杂度为O(logn)的解法. 未完待续.

[LeetCode] Factorial Trailing Zeroes 阶乘末尾0

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 creating all test cases. Hide Tags Math 这题应该是2014年年底修改该过测试样本,之前的通过

Leetcode: Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解析: 只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种

[LeetCode] 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 creating all test cases. 这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中10的个数,

LeetCode——Factorial Trailing Zeroes

题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解答: class Solution { public: int trailingZeroes(int n) { int i = 0; while (n >= 5) { i += n / 5; n /= 5; } return i; } }

LeetCode—Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 题目是非常简单的,计算一个数字递归相乘后末尾0的个数 在相乘出现2*5才有可能出现0,2一般是足够的,主要是5的个数,因为是阶乘,比如所以只要数字大于15,那么必定经过15,10,5那么肯定包含3个以上的5,比如25,那么肯定包含25,20,15,

Python3解leetcode Factorial Trailing Zeroes

问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero. Example 2: Input: 5 Output: 1 Explanation: 5! = 120, one trailing zero. Note: Your solution should be in logari