Skip to content

172. Factorial Trailing Zeroes

  • Time: $O(\log_5 n)$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  int trailingZeroes(int n) {
    return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
  }
};
1
2
3
4
5
class Solution {
  public int trailingZeroes(int n) {
    return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
  }
}
1
2
3
class Solution:
  def trailingZeroes(self, n: int) -> int:
    return 0 if n == 0 else n // 5 + self.trailingZeroes(n // 5)