File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
Factorial Trailing Zeroes Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments