题目:
给定一个单词列表 words
和一个整数 k
,返回前 k
个出现次数最多的单词。返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率, 按字典顺序 排序。
示例 1:
输入: words = ["i", "love", "leetcode", "i", "love", "coding"], k = 2 输出: ["i", "love"] 解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。 注意,按字母顺序 "i" 在 "love" 之前。
示例 2:
输入: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4 输出: ["the", "is", "sunny", "day"] 解析: "the", "is", "sunny" 和 "day" 是出现次数最多的四个单词, 出现次数依次为 4, 3, 2 和 1 次。
思路:
-
用map存储每个字符串出现次数
-
建立小根堆,用top-k解决
-
map部分写进小根堆
- 返回结果
画图解释
代码
public List<String> topKFrequent(String[] words, int k) {
//用map存储每个字符串出现次数
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < words.length; i++) {
if (!map.containsKey(words[i])) map.put(words[i], 1);
else {
int val = map.get(words[i]);
map.put(words[i], val + 1);
}
}
//建立小根堆,用top-k解决
PriorityQueue<Map.Entry<String, Integer>> pri = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> t1, Map.Entry<String, Integer> t2) {
if(t1.getValue().compareTo(t2.getValue())==0){//题目要求,形同且存前k个值时用大根堆,i和love
return t2.getKey().compareTo(t1.getKey());
}
return t1.getValue().compareTo(t2.getValue());//小根堆
}
});
//map部分写进小根堆
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (pri.size() < k) pri.offer(entry);//前k个元素存入小根堆
//words后面元素与前k个元素比较
else {
if (pri.peek().getValue() < entry.getValue()) {
pri.poll();
pri.offer(entry);
} else {
if (pri.peek().getValue() == entry.getValue()) {//如果不同的单词有相同出现频率, 按字典顺序 排序。
if (pri.peek().getKey().compareTo(entry.getKey()) > 0) {
pri.poll();
pri.offer(entry);
}
}
}
}
}
List<String> list = new ArrayList<>();
for (int i = 0; i < k; i++) {
list.add(pri.poll().getKey());
}
Collections.reverse(list);//返回由高到低
return list;
}