-
Notifications
You must be signed in to change notification settings - Fork 26
/
172.FactorialTrailingZeroes.py
44 lines (37 loc) · 1.03 KB
/
172.FactorialTrailingZeroes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Given an integer n, return the number of trailing zeroes in n!.
Follow up: Could you write a solution that works in logarithmic time
complexity?
Example:
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example:
Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Example:
Input: n = 0
Output: 0
Constraints:
- 1 <= n <= 10^4
"""
#Difficulty: Easy
#500 / 500 test cases passed.
#Runtime: 4236 ms
#Memory Usage: 14.3 MB
#Runtime: 4236 ms, faster than 11.06% of Python3 online submissions for Factorial Trailing Zeroes.
#Memory Usage: 14.3 MB, less than 16.42% of Python3 online submissions for Factorial Trailing Zeroes.
class Solution:
def trailingZeroes(self, n: int) -> int:
f = 1
while n > 0:
f = f * n
n -= 1
f = str(f)[::-1]
count = float(inf)
for i in range(1,10):
i = str(i)
if i in f:
count = min(count, f.index(i))
return count