172. Factorial Trailing Zeroes

  • 35.3%

https://leetcode.com/problems/factorial-trailing-zeroes/#/description

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.


4ms, 19.42%, July 15th, 2016

https://discuss.leetcode.com/topic/6516/my-one-line-solutions-in-3-languages

1
2
3
4
5
6
class Solution {
public:
int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
};

60ms, 31.23%, July 15th, 2016

https://discuss.leetcode.com/topic/6516/my-one-line-solutions-in-3-languages

1
2
3
4
5
6
7
class Solution(object):
def trailingZeroes(self, n):
"""
:type n: int
:rtype: int
"""
return 0 if n == 0 else n / 5 + self.trailingZeroes(n / 5)

2ms, 3.36%, July 15th, 2016

https://discuss.leetcode.com/topic/6516/my-one-line-solutions-in-3-languages

1
2
3
4
5
public class Solution {
public int trailingZeroes(int n) {
return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
}
}
谢谢你,可爱的朋友。