问题描述:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
思路:
可以先将n!进行因式分解,写成2^a*3^b*5^c……
n!尾部0的个数,可以看作求因式分解后,2的个数和5的个数
又因为5的个数一定小于2的个数,所以即求5的个数
代码:
class Solution {
public int trailingZeroes(int n) {
int result = 0;
if(n == 0) return 0;
while(n > 0){
result += n / 5;
n /= 5;
}
return result;
}
}