-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMap
More file actions
62 lines (45 loc) · 1.49 KB
/
Map
File metadata and controls
62 lines (45 loc) · 1.49 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
//692 前K个高频词
import java.awt.List;
import java.util.*;
import java.util.ArrayList;
public class Main {
public ArrayList<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> map = new HashMap<>();
for (String word : words) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
PriorityQueue<String> queue = new PriorityQueue<>(k, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (map.get(s1).equals(map.get(s2))) {
return s2.compareTo(s1);
}
return map.get(s1).compareTo(map.get(s2));
}
});
for (String key : map.keySet()) {
if (queue.size() < k) {
queue.add(key);
} else if (queue.comparator().compare(key, queue.peek()) > 0) {
queue.poll();
queue.add(key);
}
}
String[] res = new String[k];
for (int i = k - 1; i >= 0; i--) {
res[i] = queue.poll();
}
return (ArrayList<String>) Arrays.asList(res);
}
public static void main(String[] args)
{
String s="abc";
String c="vrd";
Main v=new Main();
//System.out.println(v.sea(s,c));
String q="abc";
String n="ccccccccc";
System.out.println(v.lengthOfLongestSubstring(q));
System.out.println(v.lengthOfLongestSubstring(n));
}
}