Leetcode 692 - Note
1. 题目要求
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
2. Code
先统计词频
初始化一个priorityqueue的对象,初始化的时候重写对比Comparator<? super Entry<String, Integer>> comparator ,我们用Map.entry作为接口往队列里加,如果数值相同,就对比A, B的key的字符,要做到如果A比B首字母小,就丢前面。如果不同,就比value大小
写完队列后,将hash中的key一个一个放进去,如果pq的size大于我们要求的K,就弹出最小的
排列完成
将队列中的东西按照倒叙放进linkedlist中
List<String> res = new LinkedList<>();
Map<String, Integer> freq = new HashMap<>(); for(int i = 0 ; i < words.length; i++) {
freq.put(words[i], freq.getOrDefault(words[i], 0)+1);
} PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(
// (a,b) -> a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue() - b.getValue()
(a,b) -> a.getValue()==b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue()-b.getValue()
); for(Map.Entry<String, Integer> entry : freq.entrySet()) {
pq.offer(entry);
if(pq.size() > k)
pq.poll();
} while(!pq.isEmpty()) {
res.add(0, pq.poll().getKey());
} return res;
Leetcode 692 - Note的更多相关文章
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- [leetcode]692. 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# 692. Top K Frequent Words
https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...
- Java实现 LeetCode 692 前K个高频单词(map的应用)
692. 前K个高频单词 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. 示例 1: 输入: [&qu ...
- LeetCode: Ransom Note
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] rans ...
- LeetCode Crack Note --- 1. Two Sum
Discription Given an array of integers, return indices of the two numbers such that they add up to a ...
- [leetcode]692. Top K Frequent Words频率最高的前K个单词
这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
随机推荐
- 如何在gvim中安装autoproto自动显示函数原型
cankao: http://www.vim.org/scripts/script.php?script_id=1553 注意, 在gvim中执行的命令, :foo和:!foo 的区别, 跟vim一样 ...
- Module controller in JMeter
https://qualibrate.com/blog/quality-assurance/jmeter-module-controller/ 通过组合Test Fragments 和Module C ...
- 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T4(模拟)
随便模拟下就过了qwq 然后忘了特判WA了QwQ #include <cstdio> #include <algorithm> #include <cstring> ...
- The more... the more句型
百度文库:https://wenku.baidu.com/view/a7f1067f59fb770bf78a6529647d27284a73374b.html the more ... , the m ...
- .Net Core之Swagger
1.项目生成xml 2.添加链接文件,并将属性设值为始终复制 3.添加swagger引用:Swashbuckle.AspNetCore 4.startup.cs配置swargger的xml来源: Co ...
- VirtuablBox 出错: VERR_SUPLIB_OWNER_NOT_ROOT 解决方法
刚刚把 VirtualBox 升级, 从 3.2 到 4.0.4 后,虚拟机上的系统无法运行, 提示: VERR_SUPLIB_OWNER_NOT_ROOT 查了一下,发现是因为 /opt 的 own ...
- Vue学习一:{{}}html模板使用方法
本文为博主原创,未经允许不得转载: 之前自学了vue,在项目中应用了vue,由于是第一次使用,感觉非常强大,使用也非常方便,趁有时间,总结一下vue学习过程中 各个指令的使用方法,只要掌握了vue的指 ...
- Linux命令去重统计排序
利用Linux命令进行文本按行去重并按重复次数排序 linux命令行提供了非常强大的文本处理功能,组合利用linux命令能实现好多强大的功能.本文这里举例说明如何利用Linux命令行进行文本按行去 ...
- c++ demo
#include "stdafx.h" #include <boost\version.hpp> #include <boost\config.hpp> # ...
- 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别
1. sqlserver中获取时间用getdate(),默认返回格式是2019-01-21 13:58:33.053,具体的年月日,时分秒毫米,年月日之间用短线连接,时分秒之间用冒号连接,秒和毫米之间 ...