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 = [ ...
随机推荐
- Concepts-->Migrations
https://flywaydb.org/documentation/migrations Overview With Flyway all changes to the database are c ...
- 题解——loj6279 数列分块入门3 (分块)
用set维护有序序列 或许sort也可以,但这题的前驱定义是严格小于 所以要去重 然后就是记得自己打的加法tag在query的时候一定要算上 话说这题数据有点fake啊忘了查询算上自己的标记了还有70 ...
- (转载)WinCC 卸载后 Simatic Shell 的删除
现象:WinCC卸载后,在计算机(我的电脑)中仍有Simatic Shell文件夹,双击无反应 解决:1.按Win+X,运行“regedit”,打开注册表2.在注册表中,选中HKEY_LOCAL_MA ...
- 基于OpenNetVM配置环境的发包实践
参考: openNetVM 基于OpenNetVM配置环境的发包实践 注意:本文并未对OpenNetVM的服务链进行测试,而是在借助OpenNetVM脚本环境的情况下,分别对Pktgen和MoonGe ...
- 最简单的服务器和客户机(python3的编码与解码问题)
在学习python的过程中,我越来越感觉到python2和python3之间有很多不同点,最近发现的一点就是编码问题. 在代码清单14-1和14-2中,因为作者是用python2来写得,然后我是用py ...
- 【Python】【Web开发】
# [[Web开发]] ''' 最早的软件都是运行在大型机上的,软件使用者通过“哑终端”登陆到大型机上去运行软件.后来随着PC机的兴起,软件开始主要运行在桌面上,而数据库这样的软件运行在服务器端,这种 ...
- 转 代码修改buildoption
using System;using System.IO;using UnityEditor;using UnityEngine; public class BuildPlayer : Scripta ...
- Codeforces 776E The Holmes Children
题目链接:http://codeforces.com/problemset/problem/776/E ${\because gcd(i,n-i)=1\Leftrightarrow gcd(i,n)= ...
- mkdir -p a/b 表示创建目录a,并创建目录a的子目录b
mkdir -p 命令解释 2016年01月13日 14:24:03 阅读数:742 mkdir -p a/b 表示创建目录a,并创建目录a的子目录b, 若不使用命令-p的话,想要达到同样的效果,需要 ...
- webpack踩坑之路 (2)——图片的路径与打包
刚开始用webpack的同学很容易掉进图片打包这个坑里,比如打包出来的图片地址不对或者有的图片并不能打包进我们的目标文件夹里(bundle).下面我们就来分析下在webpack项目中图片的应用场景. ...