https://leetcode.com/problems/factorial-trailing-zeroes/
求一个数的阶乘结尾有多少个连续的零,要求时间复杂度O(logN)
结尾的0由2和5相乘得到,2的个数远大于5的个数,因此转化成找5的个数。
logN复杂度有两种可能,一是二分法,二是n不断缩小常数倍(本题中就是n不断除5)
public class Solution {
public int trailingZeroes(int n) {
int res = 0;
while (n > 0) {
n /= 5;
res += n;
}
return res;
}
}