[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 221. Maximal Square
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。
43 0
LeetCode 221. Maximal Square
|
人工智能
Educational Codeforces Round 21 D.Array Division(二分)
D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output ...
957 0
|
人工智能 算法
1305 Pairwise Sum and Divide
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = 0     for i = 1 to A.
808 0
[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
1101 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
1031 0
[LeetCode]--119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? public
1058 0
[LeetCode]--118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 我是用数组做的,在草稿纸上稍微画一画应该就能找
1340 0