Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb9b599

Browse files
feat: add solutions to lcci problem: No.16.05 (doocs#1474)
No.16.05.Factorial Zeros
1 parent cbeab4e commit bb9b599

File tree

10 files changed

+215
-31
lines changed

10 files changed

+215
-31
lines changed

‎lcci/16.05.Factorial Zeros/README.md‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 题目描述
66

77
<!-- 这里写题目描述 -->
8+
89
<p>设计一个算法,算出 n 阶乘有多少个尾随零。</p>
910
<p><strong>示例 1:</strong></p>
1011
<pre><strong>输入:</strong> 3
@@ -19,22 +20,93 @@
1920
## 解法
2021

2122
<!-- 这里可写通用的实现逻辑 -->
23+
24+
**方法一:数学**
25+
26+
题目实际上是求 $[1,n]$ 中有多少个 5ドル$ 的因数。
27+
28+
我们以 130ドル$ 为例来分析:
29+
30+
1. 第 1ドル$ 次除以 5ドル,ドル得到 26ドル,ドル表示存在 26ドル$ 个包含因数 5ドル$ 的数;
31+
1. 第 2ドル$ 次除以 5ドル,ドル得到 5ドル,ドル表示存在 5ドル$ 个包含因数 5ドル^2$ 的数;
32+
1. 第 3ドル$ 次除以 5ドル,ドル得到 1ドル,ドル表示存在 1ドル$ 个包含因数 5ドル^3$ 的数;
33+
1. 累加得到从 $[1,n]$ 中所有 5ドル$ 的因数的个数。
34+
35+
时间复杂度 $O(\log n),ドル空间复杂度 $O(1)$。
36+
2237
<!-- tabs:start -->
2338

2439
### **Python3**
2540

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

2843
```python
29-
44+
class Solution:
45+
def trailingZeroes(self, n: int) -> int:
46+
ans = 0
47+
while n:
48+
n //= 5
49+
ans += n
50+
return ans
3051
```
3152

3253
### **Java**
3354

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

3657
```java
58+
class Solution {
59+
public int trailingZeroes(int n) {
60+
int ans = 0;
61+
while (n > 0) {
62+
n /= 5;
63+
ans += n;
64+
}
65+
return ans;
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
int trailingZeroes(int n) {
76+
int ans = 0;
77+
while (n) {
78+
n /= 5;
79+
ans += n;
80+
}
81+
return ans;
82+
}
83+
};
84+
```
85+
86+
### **Go**
87+
88+
```go
89+
func trailingZeroes(n int) int {
90+
ans := 0
91+
for n > 0 {
92+
n /= 5
93+
ans += n
94+
}
95+
return ans
96+
}
97+
```
3798

99+
### **TypeScript**
100+
101+
```ts
102+
function trailingZeroes(n: number): number {
103+
let ans = 0;
104+
while (n > 0) {
105+
n = Math.floor(n / 5);
106+
ans += n;
107+
}
108+
return ans;
109+
}
38110
```
39111

40112
### **...**

‎lcci/16.05.Factorial Zeros/README_EN.md‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,70 @@
3232
### **Python3**
3333

3434
```python
35-
35+
class Solution:
36+
def trailingZeroes(self, n: int) -> int:
37+
ans = 0
38+
while n:
39+
n //= 5
40+
ans += n
41+
return ans
3642
```
3743

3844
### **Java**
3945

4046
```java
47+
class Solution {
48+
public int trailingZeroes(int n) {
49+
int ans = 0;
50+
while (n > 0) {
51+
n /= 5;
52+
ans += n;
53+
}
54+
return ans;
55+
}
56+
}
57+
```
58+
59+
### **C++**
60+
61+
```cpp
62+
class Solution {
63+
public:
64+
int trailingZeroes(int n) {
65+
int ans = 0;
66+
while (n) {
67+
n /= 5;
68+
ans += n;
69+
}
70+
return ans;
71+
}
72+
};
73+
```
74+
75+
### **Go**
76+
77+
```go
78+
func trailingZeroes(n int) int {
79+
ans := 0
80+
for n > 0 {
81+
n /= 5
82+
ans += n
83+
}
84+
return ans
85+
}
86+
```
4187

88+
### **TypeScript**
89+
90+
```ts
91+
function trailingZeroes(n: number): number {
92+
let ans = 0;
93+
while (n > 0) {
94+
n = Math.floor(n / 5);
95+
ans += n;
96+
}
97+
return ans;
98+
}
4299
```
43100

44101
### **...**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int trailingZeroes(int n) {
4+
int ans = 0;
5+
while (n) {
6+
n /= 5;
7+
ans += n;
8+
}
9+
return ans;
10+
}
11+
};
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
10+
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function trailingZeroes(n: number): number {
2+
let ans = 0;
3+
while (n > 0) {
4+
n = Math.floor(n / 5);
5+
ans += n;
6+
}
7+
return ans;
8+
}

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
1. 第 3ドル$ 次除以 5ドル,ドル得到 1ドル,ドル表示存在 1ドル$ 个包含因数 5ドル^3$ 的数;
6363
1. 累加得到从 $[1,n]$ 中所有 5ドル$ 的因数的个数。
6464

65+
时间复杂度 $O(\log n),ドル空间复杂度 $O(1)$。
66+
6567
<!-- tabs:start -->
6668

6769
### **Python3**
@@ -95,27 +97,17 @@ class Solution {
9597
}
9698
```
9799

98-
### **TypeScript**
99-
100-
```ts
101-
function trailingZeroes(n: number): number {
102-
let ans = 0;
103-
while (n > 0) {
104-
n = Math.floor(n / 5);
105-
ans += n;
106-
}
107-
return ans;
108-
}
109-
```
110-
111100
### **C++**
112101

113102
```cpp
114103
class Solution {
115104
public:
116105
int trailingZeroes(int n) {
117106
int ans = 0;
118-
for (int i = 5; i <= n; i *= 5) ans += n / i;
107+
while (n) {
108+
n /= 5;
109+
ans += n;
110+
}
119111
return ans;
120112
}
121113
};
@@ -134,6 +126,19 @@ func trailingZeroes(n int) int {
134126
}
135127
```
136128

129+
### **TypeScript**
130+
131+
```ts
132+
function trailingZeroes(n: number): number {
133+
let ans = 0;
134+
while (n > 0) {
135+
n = Math.floor(n / 5);
136+
ans += n;
137+
}
138+
return ans;
139+
}
140+
```
141+
137142
### **...**
138143

139144
```

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,17 @@ class Solution {
7373
}
7474
```
7575

76-
### **TypeScript**
77-
78-
```ts
79-
function trailingZeroes(n: number): number {
80-
let ans = 0;
81-
while (n > 0) {
82-
n = Math.floor(n / 5);
83-
ans += n;
84-
}
85-
return ans;
86-
}
87-
```
88-
8976
### **C++**
9077

9178
```cpp
9279
class Solution {
9380
public:
9481
int trailingZeroes(int n) {
9582
int ans = 0;
96-
for (int i = 5; i <= n; i *= 5) ans += n / i;
83+
while (n) {
84+
n /= 5;
85+
ans += n;
86+
}
9787
return ans;
9888
}
9989
};
@@ -112,6 +102,19 @@ func trailingZeroes(n int) int {
112102
}
113103
```
114104

105+
### **TypeScript**
106+
107+
```ts
108+
function trailingZeroes(n: number): number {
109+
let ans = 0;
110+
while (n > 0) {
111+
n = Math.floor(n / 5);
112+
ans += n;
113+
}
114+
return ans;
115+
}
116+
```
117+
115118
### **...**
116119

117120
```

‎solution/0100-0199/0172.Factorial Trailing Zeroes/Solution.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
public:
33
int trailingZeroes(int n) {
44
int ans = 0;
5-
for (int i = 5; i <= n; i *= 5) ans += n / i;
5+
while (n) {
6+
n /= 5;
7+
ans += n;
8+
}
69
return ans;
710
}
811
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /