Skip to content

Commit 7dbe088

Browse files
authored
Merge pull request #242 from 0xff-dev/master
Add solution and test-cases for problem 172
2 parents 8b3a60b + aa898d8 commit 7dbe088

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [172. Factorial Trailing Zeroes][title]
2+
3+
## Description
4+
Given an integer `n`, return the number of trailing zeroes in `n!`.
5+
6+
Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
7+
8+
**Example 1:**
9+
```
10+
Input: n = 3
11+
Output: 0
12+
Explanation: 3! = 6, no trailing zero.
13+
```
14+
15+
**Example 2:**
16+
```
17+
Input: n = 5
18+
Output: 1
19+
Explanation: 5! = 120, one trailing zero.
20+
```
21+
22+
**Example 3:**
23+
```
24+
Input: n = 0
25+
Output: 0
26+
```
27+
28+
29+
## 结语
30+
31+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
32+
33+
[title]: https://leetcode.com/problems/factorial-trailing-zeroes/
34+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(n int) int {
4+
count := 0
5+
countOfFive := make(map[int]int)
6+
for step := 5; step <= n; step += 5 {
7+
countOfFive[step] = 1
8+
if c, ok := countOfFive[step/5]; ok {
9+
countOfFive[step] += c
10+
}
11+
count += countOfFive[step]
12+
}
13+
return count
14+
}
15+
16+
func Solution2(n int) int {
17+
res := 0
18+
for n > 0 {
19+
res += n / 5
20+
n /= 5
21+
}
22+
return res
523
}

leetcode/101-200/0172.Factorial-Trailing-Zeroes/Solution_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 3, 0},
17+
{"TestCase2", 5, 1},
18+
{"TestCase3", 0, 0},
19+
{"TestCase4", 150, 37},
1920
}
2021

2122
// 开始测试
@@ -26,6 +27,12 @@ func TestSolution(t *testing.T) {
2627
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2728
c.expect, got, c.inputs)
2829
}
30+
31+
got = Solution2(c.inputs)
32+
if !reflect.DeepEqual(got, c.expect) {
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
34+
c.expect, got, c.inputs)
35+
}
2936
})
3037
}
3138
}

0 commit comments

Comments
 (0)