Skip to content

Commit 173b901

Browse files
committed
一刷229
1 parent 2a545b5 commit 173b901

File tree

6 files changed

+121
-24
lines changed

6 files changed

+121
-24
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,12 +1629,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
16291629
|Medium
16301630
|
16311631

1632-
//|{counter:codes}
1633-
//|{leetcode_base_url}/majority-element-ii/[229. Majority Element II^]
1634-
//|{source_base_url}/_0229_MajorityElementII.java[Java]
1635-
//|{doc_base_url}/0229-majority-element-ii.adoc[题解]
1636-
//|Medium
1637-
//|
1632+
|{counter:codes}
1633+
|{leetcode_base_url}/majority-element-ii/[229. Majority Element II^]
1634+
|{source_base_url}/_0229_MajorityElementII.java[Java]
1635+
|{doc_base_url}/0229-majority-element-ii.adoc[题解]
1636+
|Medium
1637+
|
16381638

16391639
|{counter:codes}
16401640
|{leetcode_base_url}/kth-smallest-element-in-a-bst/[230. Kth Smallest Element in a BST^]

docs/0169-majority-element.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ You may assume that the array is non-empty and the majority element always exist
3232

3333
哈希计数法最易想到,摩尔投票法最妙!
3434

35+
摩尔投票法更刺激的使用在 xref:0229-majority-element-ii.adoc[229. 多数元素 II]。
36+
3537
image::images/0169-01.png[{image_attr}]
3638

3739

docs/0229-majority-element-ii.adoc

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,72 @@
11
[#0229-majority-element-ii]
2-
= 229. Majority Element II
2+
= 229. 多数元素 II
33

4-
{leetcode}/problems/majority-element-ii/[LeetCode - Majority Element II^]
4+
https://leetcode.cn/problems/majority-element-ii/[LeetCode - 229. 多数元素 II ^]
55

6-
Given an integer array of size _n_, find all elements that appear more than `⌊ n/3 ⌋` times.
6+
给定一个大小为 `n` 的整数数组,找出其中所有出现超过 `⌊ n/3 ⌋` 次的元素。
77

8-
*Note: *The algorithm should run in linear time and in O(1) space.
8+
*示例 1:*
99

10-
*Example 1:*
10+
....
11+
输入:nums = [3,2,3]
12+
输出:[3]
13+
....
1114

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* [3,2,3]
15-
*Output:* [3]
16-
----
15+
*示例 2:*
1716

18-
*Example 2:*
17+
....
18+
输入:nums = [1]
19+
输出:[1]
20+
....
1921

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* [1,1,1,3,3,2,2,2]
23-
*Output:* [1,2]
24-
----
22+
*示例 3:*
23+
24+
....
25+
输入:nums = [1,2]
26+
输出:[1,2]
27+
....
28+
29+
*提示:*
30+
31+
* `1 \<= nums.length \<= 5 * 10^4^`
32+
* `-10^9^ \<= nums[i] \<= 10^9^`
2533
34+
**进阶:**尝试设计时间复杂度为 O(n)、空间复杂度为 O(1)的算法解决此问题。
2635

2736

37+
== 思路分析
38+
39+
最简单的做是使用哈希存每个数字的出现次数,但是空间复杂度就是 stem:[O(n)]。
40+
41+
更巧妙的解法是摩尔投票法。一个数组中,最多会出现两个出现次数大于数组长度的三分之一。所以,记录两个元素和投票次数(即出现次数),与这两个元素相同,则对应元素投票次数加一,否则两个投票次数都要减一。如果出现出现次数大于三分之一,那么最后剩余投票次数大于 0 的元素,可能就是出现次数大于三分之一的。再次遍历数组,记录出现次数,最后根据次数判断是否符合题目要求。
42+
43+
摩尔投票法在 xref:0169-majority-element.adoc[169. Majority Element] 中也有使用。
44+
2845
[[src-0229]]
46+
[tabs]
47+
====
48+
一刷::
49+
+
50+
--
2951
[{java_src_attr}]
3052
----
31-
include::{sourcedir}/_0229_MajorityElementII.java[tag=answer]
53+
include::{sourcedir}/_0229_MajorityElementIi.java[tag=answer]
3254
----
55+
--
56+
57+
// 二刷::
58+
// +
59+
// --
60+
// [{java_src_attr}]
61+
// ----
62+
// include::{sourcedir}/_0229_MajorityElementIi_2.java[tag=answer]
63+
// ----
64+
// --
65+
====
66+
67+
68+
== 参考资料
3369

70+
. https://leetcode.cn/problems/majority-element-ii/solutions/123170/liang-fu-dong-hua-yan-shi-mo-er-tou-piao-fa-zui-zh/[229. 多数元素 II - 两幅动画演示摩尔投票法,最直观的理解方式^]
71+
. https://leetcode.cn/problems/majority-element-ii/solutions/1058790/qiu-zhong-shu-ii-by-leetcode-solution-y1rn/[229. 多数元素 II - 官方题解^]
72+
. https://leetcode.cn/problems/majority-element-ii/solutions/1060343/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ws0rj/[229. 多数元素 II - 【宫水三叶の相信科学系列】详解摩尔投票为何能推广到 n / k 的情况^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ include::0227-basic-calculator-ii.adoc[leveloffset=+1]
543543

544544
include::0228-summary-ranges.adoc[leveloffset=+1]
545545

546-
// include::0229-majority-element-ii.adoc[leveloffset=+1]
546+
include::0229-majority-element-ii.adoc[leveloffset=+1]
547547

548548
include::0230-kth-smallest-element-in-a-bst.adoc[leveloffset=+1]
549549

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,11 @@ endif::[]
11181118
|{doc_base_url}/0225-implement-stack-using-queues.adoc[题解]
11191119
|✅ 就是用两个队列来回倒腾。添加元素的时候,哪个空就放那个队列中,把另外一个队列里的值都倒腾过来。
11201120

1121+
|{counter:codes2503}
1122+
|{leetcode_base_url}/majority-element-ii/[229. 多数元素 II^]
1123+
|{doc_base_url}/0229-majority-element-ii.adoc[题解]
1124+
|⭕️ 最简单解法是哈希计数法。更精美的解法是摩尔投票法,通过抵消不同元素的出现次数,最后剩余下的元素可能就是符合要求元素。
1125+
11211126
|===
11221127

11231128
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _0229_MajorityElementIi {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-07-02 19:42:032
11+
*/
12+
public List<Integer> majorityElement(int[] nums) {
13+
int e1 = 1000000001, e2 = 1000000002;
14+
int v1 = 0, v2 = 0;
15+
for (int num : nums) {
16+
if (num == e1) {
17+
v1++;
18+
} else if (num == e2) {
19+
v2++;
20+
} else if (v1 == 0) {
21+
e1 = num;
22+
v1++;
23+
} else if (v2 == 0) {
24+
e2 = num;
25+
v2++;
26+
} else {
27+
v1--;
28+
v2--;
29+
}
30+
}
31+
int c1 = 0, c2 = 0;
32+
for (int num : nums) {
33+
if (v1 > 0 && num == e1) {
34+
c1++;
35+
}
36+
if (v2 > 0 && num == e2) {
37+
c2++;
38+
}
39+
}
40+
List<Integer> result = new ArrayList<>();
41+
if (c1 > nums.length / 3) {
42+
result.add(e1);
43+
}
44+
if (c2 > nums.length / 3) {
45+
result.add(e2);
46+
}
47+
return result;
48+
}
49+
// end::answer[]
50+
51+
}

0 commit comments

Comments
 (0)