172. Factorial Trailing Zeroes
給定一個整數 n,返回 n! 的末尾零的數量。
注意,n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1。
範例 1:
輸入:n = 3
輸出:0
解釋:3! = 6,沒有末尾零。
Python
class Solution:
def trailingZeroes(self, n: int) -> int:
count = 0
while n >= 5:
n //= 5
count += n
return count
16.50MB, 40ms
C++
class Solution {
public:
int trailingZeroes(int n) {
int count = 0;
while (n >= 5) {
n /= 5;
count += n;
}
return count;
}
};
7.25MB, 0ms
Javascript
/**
* @param {number} n
* @return {number}
*/
var trailingZeroes = function(n) {
let count = 0;
while (n >= 5) {
n = Math.floor(n / 5);
count += n;
}
return count;
};
48.98MB, 54ms