Skip to content

Commit 47dc67d

Browse files
committed
三刷347
1 parent d6cdf86 commit 47dc67d

File tree

6 files changed

+62
-7
lines changed

6 files changed

+62
-7
lines changed

docs/0000-sort.adoc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[#0000-sort]
22
= 排序
33

4+
image::images/sort-time-.jpg[{image_attr}]
5+
46
:sectnums:
57

68
include::0000-selection-sort.adoc[leveloffset=+1]
@@ -25,14 +27,15 @@ include::0000-radix-sort.adoc[leveloffset=+1]
2527

2628
== 参考资料
2729

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 算法^]
3133
. https://www.hello-algo.com/chapter_sorting/bucket_sort/[11.8 桶排序 - Hello 算法^]
3234
. https://www.hello-algo.com/chapter_sorting/counting_sort/[11.9 计数排序 - Hello 算法^]
3335
. https://www.hello-algo.com/chapter_sorting/radix_sort/[11.10 基数排序 - Hello 算法^]
3436
. https://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F[希尔排序 - 维基百科^]
3537
. https://blog.csdn.net/m0_70103775/article/details/127894920[希尔排序(动图+详解)^]
3638
. 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)^]
3740

3841
:sectnums!:

docs/0347-top-k-frequent-elements.adoc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[#0347-top-k-frequent-elements]
22
= 347. 前 K 个高频元素
33

4-
https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个高频元素 ^]
4+
https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个高频元素^]
55

6-
给你一个整数数组 `nums` 和一个整数 `k` ,请你返回其中出现频率前 `k` 高的元素。你可以按 *任意顺序* 返回答案。
6+
给你一个整数数组 `nums` 和一个整数 `k`,请你返回其中出现频率前 `k` 高的元素。你可以按 *任意顺序* 返回答案。
77

88
*示例 1:*
99

@@ -19,14 +19,13 @@ https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个
1919
输出: [1]
2020
....
2121

22-
2322
*提示:*
2423

2524
* `1 \<= nums.length \<= 10^5^`
2625
* `k` 的取值范围是 `[1, 数组中不相同的元素的个数]`
2726
* 题目数据保证答案唯一,换句话说,数组中前 `k` 个高频元素的集合是唯一的
2827
29-
**进阶:**你所设计算法的时间复杂度 *必须* 优于 stem:[O(nlogn)] ,其中 `n` 是数组大小。
28+
**进阶:**你所设计算法的时间复杂度 *必须* 优于 stem:[O(n*logn)] ,其中 `n` 是数组大小。
3029

3130

3231
== 思路分析
@@ -35,6 +34,7 @@ https://leetcode.cn/problems/top-k-frequent-elements/[LeetCode - 347. 前 K 个
3534

3635
Top K 问题!
3736

37+
image::images/0347-10.gif[{image_attr}]
3838

3939
[[src-0347]]
4040
[tabs]
@@ -65,6 +65,20 @@ include::{sourcedir}/_0347_TopKFrequentElements_2.java[tag=answer]
6565
include::{sourcedir}/_0347_TopKFrequentElements_21.java[tag=answer]
6666
----
6767
--
68+
69+
三刷::
70+
+
71+
--
72+
[{java_src_attr}]
73+
----
74+
include::{sourcedir}/_0347_TopKFrequentElements_3.java[tag=answer]
75+
----
76+
--
6877
====
6978

7079

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) 线性做法^]

docs/images/0347-10.gif

263 KB
Loading

docs/images/sort-time-.jpg

23.8 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,11 @@ endif::[]
15191519
|{doc_base_url}/0402-remove-k-digits.adoc[题解]
15201520
|⭕️ 单调栈。利用单调递增栈,将“当前元素”前面更大的元素都删除掉,这样就可以实现尽可能大的减小这个数字。如果数字是递增的,那么就从后面向前删除,直到删够为止。
15211521

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+
15221527
|===
15231528

15241529
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)