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;
}
    原文作者:ShutLove
    原文地址: https://www.jianshu.com/p/bb5132ce886f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞