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: 输 ...
随机推荐
- google pr值查询接口
PR(全称PageRank)是Google衡量一个网站的重要标准之一,从而影响Google搜索结果排名.Google官方提供了查询PR的API,如本站PR值:http://toolbarqueries ...
- Oracle记录学习
--基本函数--select name,count(id) from work_test group by name having count(id)>1--select upper(name) ...
- java中的类、成员变量、方法的修饰符。
http://blog.sina.com.cn/s/blog_7ffb8dd501011alw.html http://www.cnblogs.com/lixiaolun/p/4311727.html
- [转帖收集] Java注解
1.Annotation 它的作用是修饰编程元素.什么是编程元素呢?例如:包.类.构造方法.方法.成员变量等.Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和任何元数据( ...
- 如何下载ubuntu桌面,并使用
下载ubuntu,进行linux系统的操作 1.下载ubuntu 百度搜索ubuntu或直达下载链接http://cn.ubuntu.com/download/ 你可以选择,优麒麟16或者Ubuntu ...
- iOS-@2x,@3x是什么意思
当我们在公司使用UI给出的图片时候,xxx.png,xxx@2x.png,xxx@3x.png的时候,不知道分别代表着什么! 本人也是菜鸟一枚,全凭自己尝试理解而已,在尝试中得出下面的结论: xxx. ...
- mac上的webStorm上配置gitHub
一,webStorm下,首先打开Preferences; 二,在Version Control目录下,选择GitHub,填写有边的内容; 注意:填写完Login和Password的以后,点击Test一 ...
- 【IDEA】本地新建Maven项目+配置Git和GitHub+代码上传和拉取到GitHub+其他IDEA和GitHub实战
一.本地新建Maven项目并启动成功 1. 按照IDEA提供的模板,构建一个maven webapp的模板项目. 一路Next,到最后的finish.如下图. 2. 新建Tomcat,启动刚建立的项目 ...
- 手动爬虫之京东笔记本栏(ptyhon3)
import urllib.request as ur import urllib.error as ue import re # 目标网址 url = 'https://list.jd.com/li ...
- iOS接收远程通知响应方法
点击 iOS 接收远程推送主要牵扯到的方法有以下五种 (1) - (BOOL)application:(UIApplication *)application didFinishLaunchingWi ...