LeetCode:前K个高频单词【692】
LeetCode:前K个高频单词【692】
题目描述
给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。
示例 1:
输入: ["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 次。
注意:
- 假定 k 总为有效值, 1 ≤ k ≤ 集合元素数。
- 输入的单词均由小写字母组成。
题目分析
这道题是前K个高频元素【347】的进阶。难度主要增加在对结果的处理:
- 首先,结果按照单词的出现次数递减排列。
- 相同频率的单词按照子母序列排序。
我们先处理第一个问题,出现次数递减,我们知道,优先队列的是小顶堆,转换为列表后呈递增形式,我们使用集合方法将它逆转即可。
然后是第二个问题,我们在最后会对取出来的集合进行逆转操作,所以我们在设计比较器的时候,要让字母序大的在前面,比如a,b的出现次数相同,我们在比较器中先让b处在a的前面,最后逆转操作时,a就到了b的前面。
Java题解
class Solution {
public List<String> topKFrequent(String[] words, int k) {
Map<String,Integer> countMap = new HashMap<>();
for(String word:words)
countMap.put(word,countMap.getOrDefault(word,0)+1);
PriorityQueue<String> priorityQueue = new PriorityQueue<>(k, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return countMap.get(o1).equals(countMap.get(o2))?o2.compareTo(o1):countMap.get(o1)-countMap.get(o2);
}
});
for (String word : countMap.keySet()) {
priorityQueue.offer(word);
if (priorityQueue.size() > k) {
priorityQueue.poll();
}
}
List<String> ans = new ArrayList<>();
while (!priorityQueue.isEmpty()) ans.add(priorityQueue.poll());
Collections.reverse(ans);
return ans;
}
}
LeetCode:前K个高频单词【692】的更多相关文章
- Java实现 LeetCode 692 前K个高频单词(map的应用)
692. 前K个高频单词 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. 示例 1: 输入: [&qu ...
- 692. 前K个高频单词
2021-05-20 LeetCode每日一题 链接:https://leetcode-cn.com/problems/top-k-frequent-words/ 标签:堆.字典序.哈希表 题目 给一 ...
- [Swift]LeetCode692. 前K个高频单词 | Top K Frequent Words
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- [LeetCode] Top K Frequent Words 前K个高频词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- [LeetCode] Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...
- LeetCode:前K个高频元素【347】
LeetCode:前K个高频元素[347] 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- Java实现 LeetCode 347 前 K 个高频元素
347. 前 K 个高频元素 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输 ...
随机推荐
- 关联数据和formatter问题-easyui+微型持久化工具
控制器 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- 酷狗缓存文件kgtemp的加密方式
[转自:http://www.cnblogs.com/KMBlog/p/6877752.html] 首先对比了一下缓存文件和下载好的mp3文件,发现缓存文件多了1024个字节,而且对比了几个缓存文件, ...
- IdentityServer4环境部署失败分析贴(一)
前言: 在部署Idv4站点和其客户端在外网时,发现了许多问题,折腾了许久,翻看了许多代码,写个MD记录一下. 1.受保护站点提示错误: Unable to obtain configuration f ...
- bootstrap基础学习十篇
bootstrap字体图标(Glyphicons) a.什么是字体图标 字体图标是在 Web 项目中使用的图标字体.虽然,Glyphicons Halflings 需要商业许可,但是您可以通过基于项目 ...
- java小数位-DecimalFormat(转)
转载:http://blog.sina.com.cn/s/blog_93daad410101159s.html java格式化数字DecimalFormat 在输出数字时,有时需要给数字配上单位,有时 ...
- .NET中的枚举用法浅析
本文简单分析了.NET中的枚举用法.分享给大家供大家参考.具体分析如下: 我理解的枚举就是编程中约定的一个“可选值”:例如QQ的在线状态,分别有 在线,Q我吧,隐身,忙碌等等...我觉得这就是一 ...
- iOS 更改uitextfield placeholder颜色
[passwordField setValue:TPColor forKeyPath:@"_placeholderLabel.textColor"];
- Django中字典在html中的遍历
Django中字典在html中的遍历 如:在view中定义的函数 info = {'} context = {"info": info} 要在html中变量出info的信息(注意i ...
- 穿透Session 0 隔离(二)
上一篇我们已经对Session 0 隔离有了进一步认识,如果在开发过程中确实需要服务与桌面用户进行交互,可以通过远程桌面服务的API 绕过Session 0 的隔离完成交互操作. 对于简单的交互,服务 ...
- 把配置和环境解耦 eliminate “works on my machine” problems when collaborating on code with co-workers docker架构与解决的问题
Docker实践 - 懒人的技术笔记 - 博客频道 - CSDN.NET http://blog.csdn.net/lincyang/article/details/43055061 Docker直 ...