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.
解法
11!= 11*10*9*8*7*6*5*4*3*2*1,取决于含有5的个数。
10= 2*5,所以有2个5(10分解的5和5)。
public class Solution {
public int trailingZeroes(int n) {
if (n <= 0) {
return 0;
}
int sum = 0;
while (n > 0) {
sum += n / 5;
n = n / 5;
}
return sum;
}
}