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. Docker部署微服务

    部署时需要注!意!: 打开防火墙对应的应用端口!!用于外部访问!!内部互访问则不需要. 和对应数据库,缓存,消息中间件服务等的端口(当然这些服务必须先开启,它们也可使用docker部署开启) ,用于容 ...

  2. sql 之 INSERT IGNORE

    INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以保留 ...

  3. (转载)Unity3D连接本地或局域网MySQL数据库

    准备工作: 1.打开 Unity3D 安装目录,到这个路径下 Editor > Data > Mono > lib > mono > 2.0 拷贝出下图的五个动态链接库, ...

  4. shell案例题

    目录: 1.批量生成随机字符文件名案例 2.批量改名特殊案例 3.批量创建特殊要求用户案例 1.批量生成随机字符文件名案例(P359) (1).利用openssl命令来实现 #!/bin/bash # ...

  5. Lintcode415-Valid Palindrome-Medium

    Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ...

  6. 1 --- Vue 基础指令

    1.vue 指令 1.v-model  主要在表单中使用,文本框.teaxare.单选.下拉 等 2.v-text   文本渲染  类似{{}} 3.v-show  控制Dom显示隐藏   displ ...

  7. Graphics for R

    https://cran.r-project.org/web/views/Graphics.html CRAN Task View: Graphic Displays & Dynamic Gr ...

  8. 【Java】【泛型】

    泛型的优点使⽤泛型有下⾯⼏个优点:1.类型安全2.向后兼容3.层次清晰4.性能较⾼,⽤GJ(泛型JAVA)编写的代码可以为java编译器和虚拟机带来更多的类型信息,这些信息对java程序做进⼀步优化提 ...

  9. 【Django】Django-REST-Framework

    [创建简单的API] 1. cmd.exe >django-admin startproject django_rest>cd django_rest\django_rest>pyt ...

  10. overload、override、overwrite的介绍

    答:(1)overload(重载),即函数重载: ①在同一个类中: ②函数名字相同: ③函数参数不同(类型不同.数量不同,两者满足其一即可): ④不以返回值类型不同作为函数重载的条件. (2)over ...