172. Factorial Trailing Zeroes
· 閱讀時間約 1 分鐘
找階層 $n!$ 的尾端有多少個 0,也就是找 10 的個數,但因為 2 太多了,所以找有多少個 5 就好
class Solution {
public:
int trailingZeroes(int n)
{
int res = 0;
while (n)
{
res += n / 5;
n /= 5;
}
return res;
}
};
- T: $O(n)$
- S: $O(1)$