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的更多相关文章

  1. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  2. [LeetCode] Ransom Note 赎金条

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  3. [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 ...

  4. #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 ...

  5. Java实现 LeetCode 692 前K个高频单词(map的应用)

    692. 前K个高频单词 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. 示例 1: 输入: [&qu ...

  6. LeetCode: Ransom Note

    public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] rans ...

  7. 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 ...

  8. [leetcode]692. Top K Frequent Words频率最高的前K个单词

    这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...

  9. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

随机推荐

  1. tomcat使用spring-loaded实现应用热部署

    springloaded官方说明: Spring Loaded is a JVM agent for reloading class file changes whilst a JVM is runn ...

  2. ffmpeg 下载安装和简单应用

    一.ffmpeg下载 先到http://ffmpeg.org/下载ffmpeg安装文件 二.ffmpeg安装 1.解压下载完的ffmpeg-20190319-f8075b2-win64-shared. ...

  3. Java中常用的方法

    枚举: 1:DemoEnum.valueOf(String str) 从枚举类中中找字符串,如果有则返回对应枚举值 2:DemoEnum.values() 获得枚举集合 3:DemoEnum.prop ...

  4. JavaScript 拥有动态类型

    字符串.数字.布尔.数组.对象.Null.Undefined JavaScript 拥有动态类型 JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: 实例 var x // x ...

  5. mysql表分区存储过程

    本文为博主原创,未经允许不得转载: 由于数据库一张表数据量有几千万条,而且在不断增长,看见公司前辈写了一个创建表分区的存储过程,感觉 甚是牛逼,在此供自己保留学习. /*PROCEDURE creat ...

  6. 3、Python编程之MySQLdb模块(0602)

    解释器环境与选项 python解释器启动 python [options] [ -c cmd | filename | - ] [ args ] python解释器环境变量 python代码的测试.调 ...

  7. 浅谈循环中setTimeout执行顺序问题

    浅谈循环中setTimeout执行顺序问题 (下面有见解一二) 期望:开始输出一个0,然后每隔一秒依次输出1,2,3,4. for (var i = 0; i < 5; i++) { setTi ...

  8. 单域名下多子域名同时认证HTTPS

    参考: http://blog.csdn.net/wzj0808/article/details/53401101 http://www.cnblogs.com/silin6/p/5931640.ht ...

  9. Perl中命令行参数以及打开管道文件

    打开管道文件   Linux提供了管道机制,可以方便应用程序之间的数据传递.在Perl中,扣开和使用管道可采用如下形式的open函数:   open(Filehandle,”丨 CMD”);   其中 ...

  10. 【BZOJ】1831: [AHOI2008]逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1831 考虑$-1$的位置上填写的数字一定是不降的. 令${f[i][j]}$表示$DP$到 ...