算法学习记录(七十三)

60 阅读1分钟

172. 阶乘后的零

给定一个整数 n ,返回 n! 结果中尾随零的数量。

提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1

image.png 解:

const trailingZeroes = function(n) {
    let res = 0
    while (n !== 0) {
        n = ~~(n / 5)
        res += n
    }
    return res
};