Skip to content

Commit 058dd4d

Browse files
committed
feat: add solutions to lc problem: No.0172
No.0172.Factorial Trailing Zeroes
1 parent 0625393 commit 058dd4d

File tree

7 files changed

+134
-31
lines changed

7 files changed

+134
-31
lines changed

solution/0100-0199/0172.Factorial Trailing Zeroes/README.md

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
统计 5 的个数
54+
题目实际上是求 1~n 中有多少个 5 个因数。
55+
56+
我们以 130 为例来分析:
57+
58+
1. 第 1 次除以 5,得到 26,表示存在 26 个包含因数 5 的数;
59+
1. 第 2 次除以 5,得到 5,表示存在 5 个包含因数 5² 的数;
60+
1. 第 3 次除以 5,得到 1,表示存在 1 个包含因数 5³ 的数;
61+
1. 累加得到从 1~n 中所有 5 的因数的个数。
5562

5663
<!-- tabs:start -->
5764

@@ -60,27 +67,68 @@
6067
<!-- 这里可写当前语言的特殊实现逻辑 -->
6168

6269
```python
63-
70+
class Solution:
71+
def trailingZeroes(self, n: int) -> int:
72+
ans = 0
73+
while n:
74+
n //= 5
75+
ans += n
76+
return ans
6477
```
6578

6679
### **Java**
6780

6881
<!-- 这里可写当前语言的特殊实现逻辑 -->
6982

7083
```java
71-
84+
class Solution {
85+
public int trailingZeroes(int n) {
86+
int ans = 0;
87+
while (n > 0) {
88+
n /= 5;
89+
ans += n;
90+
}
91+
return ans;
92+
}
93+
}
7294
```
7395

7496
### **TypeScript**
7597

7698
```ts
7799
function trailingZeroes(n: number): number {
78-
let count = 0;
100+
let ans = 0;
79101
while (n > 0) {
80102
n = Math.floor(n / 5);
81-
count += n;
103+
ans += n;
104+
}
105+
return ans;
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int trailingZeroes(int n) {
115+
int ans = 0;
116+
for (int i = 5; i <= n; i *= 5) ans += n / i;
117+
return ans;
82118
}
83-
return count;
119+
};
120+
```
121+
122+
### **Go**
123+
124+
```go
125+
func trailingZeroes(n int) int {
126+
ans := 0
127+
for n > 0 {
128+
n /= 5
129+
ans += n
130+
}
131+
return ans
84132
}
85133
```
86134

solution/0100-0199/0172.Factorial Trailing Zeroes/README_EN.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,66 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def trailingZeroes(self, n: int) -> int:
54+
ans = 0
55+
while n:
56+
n //= 5
57+
ans += n
58+
return ans
5359
```
5460

5561
### **Java**
5662

5763
```java
58-
64+
class Solution {
65+
public int trailingZeroes(int n) {
66+
int ans = 0;
67+
while (n > 0) {
68+
n /= 5;
69+
ans += n;
70+
}
71+
return ans;
72+
}
73+
}
5974
```
6075

6176
### **TypeScript**
6277

6378
```ts
6479
function trailingZeroes(n: number): number {
65-
let count = 0;
80+
let ans = 0;
6681
while (n > 0) {
6782
n = Math.floor(n / 5);
68-
count += n;
83+
ans += n;
84+
}
85+
return ans;
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int trailingZeroes(int n) {
95+
int ans = 0;
96+
for (int i = 5; i <= n; i *= 5) ans += n / i;
97+
return ans;
6998
}
70-
return count;
99+
};
100+
```
101+
102+
### **Go**
103+
104+
```go
105+
func trailingZeroes(n int) int {
106+
ans := 0
107+
for n > 0 {
108+
n /= 5
109+
ans += n
110+
}
111+
return ans
71112
}
72113
```
73114

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
class Solution {
2-
public:
3-
int trailingZeroes(int n) {
4-
int cnt5 = 0 ;
5-
for (long long i = 5; i <= n; i *= 5)
6-
cnt5 += n/i ;
7-
return cnt5 ;
8-
}
1+
class Solution {
2+
public:
3+
int trailingZeroes(int n) {
4+
int ans = 0;
5+
for (int i = 5; i <= n; i *= 5) ans += n / i;
6+
return ans;
7+
}
98
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func trailingZeroes(n int) int {
2+
ans := 0
3+
for n > 0 {
4+
n /= 5
5+
ans += n
6+
}
7+
return ans
8+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
class Solution {
2-
public int trailingZeroes(int n) {
3-
int t = 0;
4-
while (n >= 5) {
5-
t += n / 5;
6-
n /= 5;
7-
}
8-
return t;
9-
}
1+
class Solution {
2+
public int trailingZeroes(int n) {
3+
int ans = 0;
4+
while (n > 0) {
5+
n /= 5;
6+
ans += n;
7+
}
8+
return ans;
9+
}
1010
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def trailingZeroes(self, n: int) -> int:
3+
ans = 0
4+
while n:
5+
n //= 5
6+
ans += n
7+
return ans
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function trailingZeroes(n: number): number {
2-
let count = 0;
2+
let ans = 0;
33
while (n > 0) {
44
n = Math.floor(n / 5);
5-
count += n;
5+
ans += n;
66
}
7-
return count;
7+
return ans;
88
}

0 commit comments

Comments
 (0)