Leetcode每日两题程序员

Leetcode 172. Factorial Trailing

2017-11-12  本文已影响2人  ShutLove

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;
}
上一篇 下一篇

猜你喜欢

热点阅读