File tree Expand file tree Collapse file tree 6 files changed +62
-7
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 6 files changed +62
-7
lines changed Original file line number Diff line number Diff line change 1
1
[#0000-sort]
2
2
= 排序
3
3
4
+ image::images/sort-time-.jpg[{image_attr}]
5
+
4
6
:sectnums:
5
7
6
8
include::0000-selection-sort.adoc[leveloffset=+1]
@@ -25,14 +27,15 @@ include::0000-radix-sort.adoc[leveloffset=+1]
25
27
26
28
== 参考资料
27
29
28
- . https://www.hello-algo.com/chapter_sorting/bubble_sort/[11.3 冒泡排序 - Hello 算法^]
29
- . https://www.hello-algo.com/chapter_sorting/insertion_sort/[11.4 插入排序 - Hello 算法^]
30
- . https://www.hello-algo.com/chapter_sorting/quick_sort/[11.5 快速排序 - Hello 算法^]
30
+ . https://www.hello-algo.com/chapter_sorting/bubble_sort/[11.3 冒泡排序 - Hello 算法^]
31
+ . https://www.hello-algo.com/chapter_sorting/insertion_sort/[11.4 插入排序 - Hello 算法^]
32
+ . https://www.hello-algo.com/chapter_sorting/quick_sort/[11.5 快速排序 - Hello 算法^]
31
33
. https://www.hello-algo.com/chapter_sorting/bucket_sort/[11.8 桶排序 - Hello 算法^]
32
34
. https://www.hello-algo.com/chapter_sorting/counting_sort/[11.9 计数排序 - Hello 算法^]
33
35
. https://www.hello-algo.com/chapter_sorting/radix_sort/[11.10 基数排序 - Hello 算法^]
34
36
. https://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F[希尔排序 - 维基百科^]
35
37
. https://blog.csdn.net/m0_70103775/article/details/127894920[希尔排序(动图+详解)^]
36
38
. https://www.cnblogs.com/the3times/p/12547787.html[希尔排序^]
39
+ . https://leetcode.cn/problems/top-k-frequent-elements/solutions/11201/leetcode-di-347-hao-wen-ti-qian-k-ge-gao-pin-yuan-/[347. 前 K 个高频元素 - 力扣(LeetCode)^]
37
40
38
41
:sectnums!:
Original file line number Diff line number Diff line change 1
1
[#0347-top-k-frequent-elements]
2
2
= 347. 前 K 个高频元素
3
3
4
- https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个高频元素 ^]
4
+ https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个高频元素^]
5
5
6
- 给你一个整数数组 `nums` 和一个整数 `k` ,请你返回其中出现频率前 `k` 高的元素。你可以按 *任意顺序* 返回答案。
6
+ 给你一个整数数组 `nums` 和一个整数 `k` ,请你返回其中出现频率前 `k` 高的元素。你可以按 *任意顺序* 返回答案。
7
7
8
8
*示例 1:*
9
9
@@ -19,14 +19,13 @@ https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个
19
19
输出: [1]
20
20
....
21
21
22
-
23
22
*提示:*
24
23
25
24
* `1 \<= nums.length \<= 10^5^`
26
25
* `k` 的取值范围是 `[1, 数组中不相同的元素的个数]`
27
26
* 题目数据保证答案唯一,换句话说,数组中前 `k` 个高频元素的集合是唯一的
28
27
29
- **进阶:** 你所设计算法的时间复杂度 *必须* 优于 stem:[O(nlogn )] ,其中 `n` 是数组大小。
28
+ **进阶:** 你所设计算法的时间复杂度 *必须* 优于 stem:[O(n*logn )] ,其中 `n` 是数组大小。
30
29
31
30
32
31
== 思路分析
@@ -35,6 +34,7 @@ https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个
35
34
36
35
Top K 问题!
37
36
37
+ image::images/0347-10.gif[{image_attr}]
38
38
39
39
[[src-0347]]
40
40
[tabs]
@@ -65,6 +65,20 @@ include::{sourcedir}/_0347_TopKFrequentElements_2.java[tag=answer]
65
65
include::{sourcedir}/_0347_TopKFrequentElements_21.java[tag=answer]
66
66
----
67
67
--
68
+
69
+ 三刷::
70
+ +
71
+ --
72
+ [{java_src_attr}]
73
+ ----
74
+ include::{sourcedir}/_0347_TopKFrequentElements_3.java[tag=answer]
75
+ ----
76
+ --
68
77
====
69
78
70
79
80
+ == 参考资料
81
+
82
+ . https://leetcode.cn/problems/top-k-frequent-elements/solutions/402568/qian-k-ge-gao-pin-yuan-su-by-leetcode-solution/[347. 前 K 个高频元素 - 官方题解^]
83
+ . https://leetcode.cn/problems/top-k-frequent-elements/solutions/11201/leetcode-di-347-hao-wen-ti-qian-k-ge-gao-pin-yuan-/[347. 前 K 个高频元素 - 两种解法^]
84
+ . https://leetcode.cn/problems/top-k-frequent-elements/solutions/3655287/tong-pai-xu-on-xian-xing-zuo-fa-pythonja-oqq2/[347. 前 K 个高频元素 - 桶排序,O(n) 线性做法^]
Original file line number Diff line number Diff line change @@ -1519,6 +1519,11 @@ endif::[]
1519
1519
|{doc_base_url} /0402-remove-k-digits.adoc[题解]
1520
1520
|⭕️ 单调栈。利用单调递增栈,将“当前元素”前面更大的元素都删除掉,这样就可以实现尽可能大的减小这个数字。如果数字是递增的,那么就从后面向前删除,直到删够为止。
1521
1521
1522
+ |{counter:codes2503}
1523
+ |{leetcode_base_url} /top-k-frequent-elements/[347. 前 K 个高频元素^]
1524
+ |{doc_base_url} /0347-top-k-frequent-elements.adoc[题解]
1525
+ |✅ Top K 问题,优先队列;桶排序;也可以利用快速选择。
1526
+
1522
1527
|===
1523
1528
1524
1529
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change
1
+ package com .diguage .algo .leetcode ;
2
+
3
+ import java .util .*;
4
+
5
+ public class _0347_TopKFrequentElements_3 {
6
+ // tag::answer[]
7
+
8
+ /**
9
+ * @author D瓜哥 · https://www.diguage.com
10
+ * @since 2025-09-12 22:32:37
11
+ */
12
+ public int [] topKFrequent (int [] nums , int k ) {
13
+ Map <Integer , Integer > map = new HashMap <>();
14
+ for (int num : nums ) {
15
+ map .put (num , map .getOrDefault (num , 0 ) + 1 );
16
+ }
17
+ PriorityQueue <Integer > queue = new PriorityQueue <>(Comparator .comparingInt (map ::get ));
18
+ for (Integer key : map .keySet ()) {
19
+ if (queue .size () < k ) {
20
+ queue .offer (key );
21
+ } else if (map .get (key ) > map .get (queue .peek ())) {
22
+ queue .poll ();
23
+ queue .offer (key );
24
+ }
25
+ }
26
+ int [] result = new int [k ];
27
+ for (int i = 0 ; i < k ; i ++) {
28
+ result [i ] = queue .poll ();
29
+ }
30
+ return result ;
31
+ }
32
+ // end::answer[]
33
+ }
You can’t perform that action at this time.
0 commit comments