Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
我们对n!分解质因子,n!=2^x+3^y+5^z+...,结尾的0一定是1个2和1个5相乘得到的。很显然,这里因子5的个数小于因子2的个数,所以只要计算n的阶乘分解质因子得到的5的个数即可。
class Solution {
public:
int trailingZeroes(int n) {
int s=0;
while(n){
s+=n/5;
n/=5;
}
return s;
}
};