Skip to content

Commit b08fd9f

Browse files
committed
172
1 parent 5b02d31 commit b08fd9f

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

SUMMARY.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
* [98. Validate Binary Search Tree](leetCode-98-Validate-Binary-Search-Tree.md)
103103
* [99. Recover Binary Search Tree](leetcode-99-Recover-Binary-Search-Tree.md)
104104
* [100. Same Tree](leetcode-100-Same-Tree.md)
105-
* [101 题到 171题](leetcode-101-200.md)
105+
* [101 题到 172题](leetcode-101-200.md)
106106
* [101. Symmetric Tree](leetcode-101-Symmetric-Tree.md)
107107
* [102. Binary Tree Level Order Traversal](leetcode-102-Binary-Tree-Level-Order-Traversal.md)
108108
* [103. Binary Tree Zigzag Level Order Traversal](leetcode-103-Binary-Tree-Zigzag-Level-Order-Traversal.md)
@@ -166,4 +166,5 @@
166166
* [167. Two Sum II - Input array is sorted](leetcode-167-Two-SumII-Input-array-is-sorted.md)
167167
* [168. Excel Sheet Column Title](leetcode-168-Excel-Sheet-Column-Title.md)
168168
* [169. Majority Element](leetcode-169-Majority-Element.md)
169-
* [171. Excel Sheet Column Number](leetcode-171-Excel-Sheet-Column-Number.md)
169+
* [171. Excel Sheet Column Number](leetcode-171-Excel-Sheet-Column-Number.md)
170+
* [172. Factorial Trailing Zeroes](leetcode-172-Factorial-Trailing-Zeroes.md)

leetcode-101-200.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,6 @@
124124

125125
<a href="leetcode-169-Majority-Element.html">169. Majority Element</a>
126126

127-
<a href="leetcode-171-Excel-Sheet-Column-Number.html">171. Excel Sheet Column Number</a>
127+
<a href="leetcode-171-Excel-Sheet-Column-Number.html">171. Excel Sheet Column Number</a>
128+
129+
<a href="leetcode-172-Factorial-Trailing-Zeroes.html">172. Factorial Trailing Zeroes</a>
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 题目描述(简单难度)
2+
3+
![](https://windliang.oss-cn-beijing.aliyuncs.com/172.jpg)
4+
5+
给定一个数,求出一个数的阶乘末尾有多少个 0。
6+
7+
# 解法一
8+
9+
之前小红书面试的时候碰到的一道题,没想到又是 leetcode 的原题。这种没有通用解法的题,完全依靠于对题目的分析理解了,自己当时也是在面试官的提示下慢慢出来的,要是想不到题目的点,还是比较难做的。
10+
11+
首先肯定不能依赖于把阶乘算出来再去判断有多少个零了,因为阶乘很容易就溢出了,所以先一步一步理一下思路吧。
12+
13+
首先末尾有多少个 `0` ,只需要给当前数乘以一个 `10` 就可以加一个 `0`
14+
15+
再具体对于 `5!`,也就是 `5 * 4 * 3 * 2 * 1 = 120`,我们发现结果会有一个 `0`,原因就是 `2``5` 相乘构成了一个 `10`。而对于 `10` 的话,其实也只有 `2 * 5` 可以构成,所以我们只需要找有多少对 `2/5`
16+
17+
我们把每个乘数再稍微分解下,看一个例子。
18+
19+
`11! = 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 11 * (2 * 5) * 9 * (4 * 2) * 7 * (3 * 2) * (1 * 5) * (2 * 2) * 3 * (1 * 2) * 1 `
20+
21+
对于含有 `2` 的因子的话是 `1 * 2, 2 * 2, 3 * 2, 4 * 2 ...`
22+
23+
对于含有 `5` 的因子的话是 `1 * 5, 2 * 5...`
24+
25+
含有 `2` 的因子每两个出现一次,含有 `5` 的因子每 `5` 个出现一次,所有 `2` 出现的个数远远多于 `5`,换言之找到一个 `5`,一定能找到一个 `2` 与之配对。所以我们只需要找有多少个 `5`
26+
27+
直接的,我们只需要判断每个累乘的数有多少个 `5` 的因子即可。
28+
29+
```java
30+
public int trailingZeroes(int n) {
31+
int count = 0;
32+
for (int i = 1; i <= n; i++) {
33+
int N = i;
34+
while (N > 0) {
35+
if (N % 5 == 0) {
36+
count++;
37+
N /= 5;
38+
} else {
39+
break;
40+
}
41+
}
42+
}
43+
return count;
44+
45+
}
46+
```
47+
48+
![](https://windliang.oss-cn-beijing.aliyuncs.com/172_2.jpg)
49+
50+
但发生了超时,我们继续分析。
51+
52+
对于一个数的阶乘,就如之前分析的,`5` 的因子一定是每隔 `5` 个数出现一次,也就是下边的样子。
53+
54+
`n! = 1 * 2 * 3 * 4 * (1 * 5) * ... * (2 * 5) * ... * (3 * 5) *... * n`
55+
56+
因为每隔 `5` 个数出现一个 `5`,所以计算出现了多少个 `5`,我们只需要用 `n/5` 就可以算出来。
57+
58+
但还没有结束,继续分析。
59+
60+
`... * (1 * 5) * ... * (1 * 5 * 5) * ... * (2 * 5 * 5) * ... * (3 * 5 * 5) * ... * n`
61+
62+
每隔 `25` 个数字,出现的是两个 `5`,所以除了每隔 `5` 个数算作一个 `5`,每隔 `25` 个数,还需要多算一个 `5`
63+
64+
也就是我们需要再加上 `n / 25``5`
65+
66+
同理我们还会发现每隔 `5 * 5 * 5 = 125 ` 个数字,会出现 `3``5`,所以我们还需要再加上 `n / 125`
67+
68+
综上,规律就是每隔 `5` 个数,出现一个 `5`,每隔 `25` 个数,出现 `2``5`,每隔 `125` 个数,出现 `3``5`... 以此类推。
69+
70+
最终 `5` 的个数就是 `n / 5 + n / 25 + n / 125 ...`
71+
72+
写程序的话,如果直接按照上边的式子计算,分母可能会造成溢出。所以算 `n / 25` 的时候,我们先把 `n` 更新,`n = n / 5`,然后再计算 `n / 5` 即可。后边的同理。
73+
74+
```java
75+
public int trailingZeroes(int n) {
76+
int count = 0;
77+
while (n > 0) {
78+
count += n / 5;
79+
n = n / 5;
80+
}
81+
return count;
82+
}
83+
```
84+
85+
#
86+
87+
更偏向于数学题,主要是对问题的归纳总结。

0 commit comments

Comments
 (0)