Leetcode 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.

思路:最后的0有多少个取决于阶乘累加的过程中有多少个2*5,而2出现的概率要大于5,所以只需要统计5的个数。

public int trailingZeroes(int n) {
    int res = 0;
    while (n > 0) {
        n /= 5;
        res += n;
    }
    return res;
}

你可能感兴趣的:(Leetcode 172. Factorial Trailing Zeroes)