[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.

数学题,只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种,5和5相乘的结果,所以,还要看n/5里面有多少个5,也就相当于看n里面有多少个25,还有125,625.

 1 class Solution {
 2 public:
 3     int trailingZeroes(int n) {
 4         int res = 0;
 5         while (n) {
 6             res += n / 5;
 7             n /= 5;
 8         }
 9         return res;
10     }
11 };
原文地址:https://www.cnblogs.com/easonliu/p/4218220.html