-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_692_1.java
More file actions
68 lines (58 loc) · 2.38 KB
/
LeetCode_692_1.java
File metadata and controls
68 lines (58 loc) · 2.38 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
package week02;
import java.util.*;
/**
* @创建人 luoxiang
* @创建时间 2019/6/12 9:30
* @描述 LeetCode : 692. 前K个高频单词 https://leetcode-cn.com/problems/top-k-frequent-words/
*/
public class TopKFrequentWords692 {
public static void main(String[] args) {
String[] strs = new String[]{"i", "love", "leetcode", "i", "love", "coding"};
String[] strs2 = new String[]{"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"};
final List<String> list = new TopKFrequentWords692().topKFrequent(strs, 2);
final List<String> list2 = new TopKFrequentWords692().topKFrequent(strs2, 4);
for (String s : list) {
System.out.print(s + ",");
}
System.out.println();
for (String s : list2) {
System.out.print(s + ",");
}
}
/**
* Method 1 : 使用优先队列, 需要重写 优先队列的比较方式
* 时间复杂度 : O(N) ;
*/
public List<String> topKFrequent(String[] words, int k) {
Map<String, Integer> map = new HashMap<>();
for (String word : words) map.put(word, map.getOrDefault(word, 0) + 1);
PriorityQueue<String> pq = new PriorityQueue(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return map.get(o1) == map.get(o2) ? o1.compareTo(o2) : map.get(o2) - map.get(o1);
}
}
);
List<String> list = new LinkedList<>();
for (String entry : map.keySet()) pq.offer(entry);
for (int i = 0; i < k; i++) list.add(pq.poll());
return list;
}
public List<String> topKFrequent2(String[] words, int k) {
Map<String, Integer> map = new HashMap<>();
for (String word : words) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(
(a, b) -> a.getValue() == b.getValue() ? a.getKey().compareTo(b.getKey()) : b.getValue() - a.getValue()
);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
pq.offer(entry);
}
List<String> list = new LinkedList<>();
for (int i = 0; i < k; i++) {
list.add(pq.poll().getKey());
}
return list;
}
}