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. 使用msi自动安装系统

    在实际生活中, 还是要尽量使用 自动化 脚本 等来处理/执行问题, 那样更快更省力省时间 要多使用 网络工具, 网络工具在 管理/ 使用网络的过程 中还是很有用的. 要有这种 "多使用网络工 ...

  2. P2234 [HNOI2002]营业额统计(Splay树)题解

    思路:Splay数查找前驱后继 代码: #include<iostream> #include<cstdio> #include<cstring> #include ...

  3. LuoguP3674 小清新人渣的本愿 && BZOJ4810: [Ynoi2017]由乃的玉米田

    题目地址 小清新人渣的本愿 [Ynoi2017]由乃的玉米田 所以这两题也就输出不一样而已 题解 这种lxl的题还是没修改操作的题基本就是莫队 分开考虑每个询问 1.减法 \(a-b=x⇒a=b+x\ ...

  4. 今天的任务--git练习

    克隆远程仓库项目 从版本控制中选择git 填写地址和本地目录,test测试成功后点击clone 克隆完成回到主界面,点击open打开刚才克隆的项目 git操作 添加文件py1.html 打开命令行 新 ...

  5. P3041 [USACO12JAN]视频游戏的连击Video Game Combos

    思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...

  6. Derek解读Bytom源码-P2P网络 地址簿

    作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...

  7. JavaScript 调试常见报错以及原因

    JavaScript 调试常见报错以及原因 测试环境 chrome 版本 66.0.3359.170(正式版本) (64 位) TypeError 类型错误 不是操作符所接受的数据类型. //---- ...

  8. 解析Django路由层URLconf

    目录: 一  Django中路由的作用 二  路由的分组 三  路由分发 四  反向解析 五  名称空间 六  Django2.0版的path 一.Django中路由的作用 URL配置(URLconf ...

  9. JavaScript——类型检测

    要检测一个变量是否是基本数据类型,可以用 Typeof 操作符.如果我们想知道它是什么类型的对象,我们可以用instanceof 操作符,语法如下所示: result=variable instanc ...

  10. HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)

    http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道, ...