Skip to content

Commit 7a1bbb8

Browse files
committed
finish 172
1 parent a0c6a20 commit 7a1bbb8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 172. Factorial Trailing Zeroes
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Math.
5+
- Similar Questions: Number of Digit One, Preimage Size of Factorial Zeroes Function.
6+
7+
## Problem
8+
9+
Given an integer *n*, return the number of trailing zeroes in *n*!.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: 3
15+
Output: 0
16+
Explanation: 3! = 6, no trailing zero.
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: 5
23+
Output: 1
24+
Explanation: 5! = 120, one trailing zero.
25+
```
26+
27+
**Note: **Your solution should be in logarithmic time complexity.
28+
29+
## Solution
30+
31+
```javascript
32+
/**
33+
* @param {number} n
34+
* @return {number}
35+
*/
36+
var trailingZeroes = function(n) {
37+
if (n === 0) return 0;
38+
return Math.floor(n / 5) + trailingZeroes(Math.floor(n / 5));
39+
};
40+
```
41+
42+
**Explain:**
43+
44+
nope.
45+
46+
**Complexity:**
47+
48+
* Time complexity : O(log(n)).
49+
* Space complexity : O(1).

0 commit comments

Comments
 (0)