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 = [ ...
随机推荐
- Read.csv: some rows are missing
read.csv in R doesn't import all rows from csv file The OP indicates that the problem is caused by q ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- 【OData】使用Odata获取数据之后再次获取可能得不到最新的数据问题记录
工作上遇到个问题是关于系统后台数据库更新了某数据后, 前台界面刷新显示的不是最新的数据.但是大约10分后再次刷新就能显示新的数据,或者重启IIS等web server host. 最开始认为可能是因为 ...
- kubernetes 实战4_命令_Configure Pods and Containers
Configure Service Accounts for Pods A service account provides an identity for processes that run in ...
- Codeforces 781C Underground Lab
题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...
- 【BZOJ】4011: [HNOI2015]落忆枫音
题目链接:http://blog.csdn.net/popoqqq/article/details/45194103 写代码的时候也没有很清晰....具体看这里吧 #include<iostre ...
- python函数名称
一.第一类对象, 函数名的使用 函数名就是变量名, 函数名存储的是函数的内存地址 变量的命名规范: 由数字, 字母, 下划线组成 不能是数字开头, 更不能是纯数字 不能用关键字 不要太长 要有意义 不 ...
- python,函数的基本用法
一.函数 函数的概念:对功能或者动作的封装可以帮我们把一段公共的代码提取出来 语法如下 def 函数名(形参): 函数体 函数名(实参) # 函数名() def yue(): print(" ...
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
关于出现这个·问题的原因貌似也是多种多样的? 在stack overflow上的帖子如下:https://stackoverflow.com/questions/43186315/tomcat-404 ...
- const修饰函数
#include <iostream> using namespace std; class A { public: A(int age); void printAge() const; ...