Skip to content

Commit 1007f75

Browse files
Add files via upload
1 parent 9775a20 commit 1007f75

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 题目要求log的时间复杂度
2+
// Runtime: 8 ms, faster than 100.00% of C++ online submissions for Factorial Trailing Zeroes.
3+
// Memory Usage: 10.9 MB, less than 81.08% of C++ online submissions for Factorial Trailing Zeroes.
4+
5+
class Solution
6+
{
7+
public:
8+
int trailingZeroes(int n)
9+
{
10+
int sum = 0;
11+
while (n > 0)
12+
{
13+
sum += (n / 5);
14+
n /= 5;
15+
}
16+
return sum;
17+
}
18+
};
19+
20+
// Runtime: 12 ms, faster than 54.92% of C++ online submissions for Factorial Trailing Zeroes.
21+
// Memory Usage: 11 MB, less than 48.65% of C++ online submissions for Factorial Trailing Zeroes.
22+
class Solution
23+
{
24+
public:
25+
int trailingZeroes(int n)
26+
{
27+
int sum = 0;
28+
// 注意这里为了防止溢出而使用了long
29+
for (long temp = 5; temp <= n; temp *= 5)
30+
sum += (n / temp);
31+
return sum;
32+
}
33+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Runtime: 40 ms, faster than 100.00% of Python3 online submissions for Factorial Trailing Zeroes.
2+
# Memory Usage: 12.5 MB, less than 46.24% of Python3 online submissions for Factorial Trailing Zeroes.
3+
4+
class Solution:
5+
def trailingZeroes(self, n: 'int') -> 'int':
6+
sum = 0
7+
while n > 0:
8+
sum += (n // 5)
9+
n //= 5
10+
return sum

0 commit comments

Comments
 (0)