Skip to content

Commit d2e3bdc

Browse files
committed
Added Majority Element and Factorial Trailing Zeroes
1 parent f2eee94 commit d2e3bdc

7 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎java/SUMMARY.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [Candy](linear-list/array/candy.md)
2525
* [Single Number](linear-list/array/single-number.md)
2626
* [Single Number II](linear-list/array/single-number-ii.md)
27+
* [Majority Element](linear-list/array/majority-element.md)
2728
* [单链表](linear-list/linked-list/README.md)
2829
* [Add Two Numbers](linear-list/linked-list/add-two-numbers.md)
2930
* [Reverse Linked List II](linear-list/linked-list/reverse-linked-list-ii.md)
@@ -181,3 +182,4 @@
181182
* [Text Justification](simulation/text-justification.md)
182183
* [Max Points on a Line](simulation/max-points-on-a-line.md)
183184
* [Fraction to Recurring Decimal](simulation/fraction-to-recurring-decimal.md)
185+
* [Factorial Trailing Zeroes](simulation/factorial-trailing-zeroes.md)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Factorial Trailing Zeroes
2+
// Time Complexity: O(nlogn), Space Complexity: O(1)
3+
public class Solution {
4+
public int trailingZeroes(int n) {
5+
int result = 0;
6+
7+
for (int i = 1; i <= n; ++i) {
8+
int j = i;
9+
while (j % 5 == 0) {
10+
++result;
11+
j /= 5;
12+
}
13+
}
14+
return result;
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Factorial Trailing Zeroes
2+
// Time Complexity: O(logn), Space Complexity: O(1)
3+
public class Solution {
4+
public int trailingZeroes(int n) {
5+
int result = 0;
6+
7+
while (n > 0) {
8+
result += n / 5;
9+
n /= 5;
10+
}
11+
return result;
12+
}
13+
}

‎java/code/majority-element-1.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Majority Element
2+
// Time Complexity: O(nlogn), Space Complexity: O(1)
3+
public class Solution {
4+
public int majorityElement(int[] nums) {
5+
Arrays.sort(nums);
6+
return nums[nums.length/2];
7+
}
8+
}

‎java/code/majority-element-2.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Majority Element
2+
// Time Complexity: O(n), Space Complexity: O(1)
3+
public class Solution {
4+
public int majorityElement(int[] nums) {
5+
int result = 0;
6+
int count = 0;
7+
8+
for (int x : nums) {
9+
if (count == 0) {
10+
result = x;
11+
count = 1;
12+
} else if (result == x) {
13+
++count;
14+
} else {
15+
--count;
16+
}
17+
}
18+
return result;
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Majority Element
2+
3+
4+
### 描述
5+
6+
Given an array of size n, find the majority element. The majority element is the element that appears more than `⌊ n/2 ⌋` times.
7+
8+
You may assume that the array is non-empty and the majority element always exist in the array.
9+
10+
11+
### 分析
12+
13+
这题最简单的解法,先把数组排序,`O(nlogn)`,然后从头到尾扫描一遍,找出最长的连续子串。
14+
15+
由于超过`⌊ n/2 ⌋`次,可以对上面的方法改进一下,排序后,不需要扫描,直接返回中间那个元素,`nums[n/2]`,肯定就是答案。
16+
17+
上述两个方法都是 `O(nlogn)`的,本题有一个线性解法。由于超过`⌊ n/2 ⌋`,可以用相抵消的思想,凡是和最多元素不相等的,就抵消,最后剩下的一定就是最多的那个元素。
18+
19+
20+
### 解法1 排序
21+
22+
{% if book.java %}
23+
{% codesnippet "./code/majority-element-1."+book.suffix, language=book.suffix %}{% endcodesnippet %}
24+
{% endif %}
25+
26+
27+
### 解法2 线性解法
28+
29+
{% if book.java %}
30+
{% codesnippet "./code/majority-element-2."+book.suffix, language=book.suffix %}{% endcodesnippet %}
31+
{% endif %}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Factorial Trailing Zeroes
2+
3+
### 描述
4+
5+
Given an integer `n`, return the number of trailing zeroes in `n!`.
6+
7+
Note: Your solution should be in logarithmic time complexity.
8+
9+
10+
### 分析
11+
12+
对任意一个正整数n进行质因数分解,$$n = 2^x*3^y*5^z...$$ ,末尾0的个数M与2和5的个数即X、Z有关,每一对2和5都可以得到10,故`M=min(X,Z)`。又因为能被2整除的数出现的频率要比能被5整除的数出现的频率高,所以`M=Z`。所以只要计算出Z,就可以得到`n!`的末尾0的个数。
13+
14+
15+
### 解法1
16+
17+
{% if book.java %}
18+
```java
19+
// Factorial Trailing Zeroes
20+
// TLE
21+
// Time Complexity: O(nlogn), Space Complexity: O(1)
22+
public class Solution {
23+
public int trailingZeroes(int n) {
24+
int result = 0;
25+
26+
for (int i = 1; i <= n; ++i) {
27+
int j = i;
28+
while (j % 5 == 0) {
29+
++result;
30+
j /= 5;
31+
}
32+
}
33+
return result;
34+
}
35+
}
36+
```
37+
{% endif %}
38+
39+
40+
### 解法2
41+
42+
上面的解法会超时,可以优化一下。
43+
44+
可以用公式计算出末尾0的个数,$$Z=N/5 + N/5^2 + N/5^3 + ...$$ ,$$N/5$$ 表示从1到N中能被5整除的数的个数,由于每个数都能贡献一个5,意味着能贡献一个0。$$N/5^2$$ 表示从1到N中能被 $$5^2$$ 整除的数的个数,每个数都能贡献2个5,意味着能贡献两个0,不过由于其中一次已经包含在 $$N/5$$ 中了,只能再贡献一个0,依次类推。
45+
46+
{% if book.java %}
47+
{% codesnippet "./code/factorial-trailing-zeroes-2."+book.suffix, language=book.suffix %}{% endcodesnippet %}
48+
{% endif %}

0 commit comments

Comments
 (0)