-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFactorialTrailingZeroes.java
More file actions
46 lines (40 loc) · 1 KB
/
Copy pathFactorialTrailingZeroes.java
File metadata and controls
46 lines (40 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package math;
// Source : https://leetcode.com/problems/factorial-trailing-zeroes/
// Id : 172
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-11-05
// Topic : Math
// Level : Easy
// Other :
// Tips :
// Result : 100.00% 100.00%
public class FactorialTrailingZeroes {
// TLE
public int trailingZeroes(int n) {
int count = 0;
for (int i = 5; i <= n; i += 5) {
int currentFactor = i;
while (currentFactor % 5 == 0) {
count++;
currentFactor /= 5;
}
}
return count;
}
/**
* 因为每隔 5 个数出现一个 5,出现了多少个 5可以用 n/5 计算。
* 对于5的多次幂 需要除多次
*
* @param n
* @return
*/
public int trailingZeroes1(int n) {
// public int trailingZeroes(int n) {
int count = 0;
while (n > 0) {
count += n / 5;
n /= 5;
}
return count;
}
}