解题思路:只有所有阶乘的数里面出现因子2和5相乘才会有0出现。又容易观察到2的个数总是大于5的个数,所以本题转化为求所有小于等于当前n的正整数中因子5的个数。例如n = 30。n/5 = 6。此处的6并不能代表5的个数,因为25中包含两个5。所以求5的个数可以表示成SUM(N/5^1, N/5^2, N/5^3…)。
public class Solution {
public int trailingZeroes(int n) {
if(n<1) return 0;
int c = 0;
while(n/5 != 0) {
n /= 5;
c += n;
}
return c;
}
}