-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path692.top-k-frequent-words.java
45 lines (40 loc) · 1.14 KB
/
692.top-k-frequent-words.java
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
/*
* @lc app=leetcode id=692 lang=java
*
* [692] Top K Frequent Words
*/
class Solution {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> map = new HashMap<>();
for (String word : words) {
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
}
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (map.get(o1) == map.get(o2)) {
return o2.compareTo(o1); // 如果出现次数一样,按单词字母顺序排序
}
return map.get(o1) - map.get(o2);
}
};
// 小顶堆 topk frequent elements
Queue<String> queue = new PriorityQueue<>(comparator);
for (String key : map.keySet()) {
queue.offer(key);
if (queue.size() > k) {
queue.poll();
}
}
// 输出频率从高到低,每次从小顶堆弹出堆顶元素,插入列表最前面
List<String> result = new LinkedList<>();
while (!queue.isEmpty()) {
result.add(0, queue.poll());
}
return result;
}
}