forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution347.java
More file actions
71 lines (68 loc) · 2.1 KB
/
Solution347.java
File metadata and controls
71 lines (68 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.*;
public class Solution347 {
/**
* 找出高频的前n个数
* 1. 小根堆
*
* @param nums 数组
* @param k 数值
* @return 结果
*/
public int[] topKFrequent(int[] nums, int k) {
if (k == 0) return new int[0];
Map<Integer, Integer> map = new HashMap<>(nums.length);
for (int num: nums) {
map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1);
}
PriorityQueue<int[]> heap = new PriorityQueue<>((a1, a2) -> a1[1] - a2[1]);
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
heap.offer(new int[] {entry.getKey(), entry.getValue()});
if (heap.size() > k) {
heap.poll();
}
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = heap.poll()[0];
}
return result;
}
/**
* 找出高频的前n个数
* 2. 桶排序
*
* @param nums 数组
* @param k 数值
* @return 结果
*/
public int[] topKFrequent2(int[] nums, int k) {
if (k == 0) return new int[0];
Map<Integer, Integer> map = new HashMap<>(nums.length);
for (int num: nums) {
map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1);
}
// 因为频率最大可以等于长度,所以此处要+1
List<Integer>[] list = new List[nums.length + 1];
for (int key: map.keySet()) {
int i = map.get(key);
if (list[i] == null) {
list[i] = new ArrayList<>();
}
list[i].add(key);
}
int[] result = new int[k];
int index = 0;
for (int i = list.length - 1; i >= 0 && index < k; i--) {
if (list[i] == null) {
continue;
}
for (Integer num: list[i]) {
result[index++] = num;
}
}
return result;
}
public static void main(String[] args) {
System.out.println(new Solution347().topKFrequent2(new int[] {1,1,1,2,2,3}, 2));
}
}