LeetCode - Top K Frequent Words
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. Example 1:
Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Example 2:
Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.
Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Input words contain only lowercase letters.
Follow up:
Try to solve it in O(n log k) time and O(n) extra space.
先用 hashmap 存储string 和出现次数的映射, 然后insert并维持一个size为K的priorityqueue (注意要自己定义compare 函数),最后会得到top Kth words 但是是从小到大排序,注意要reverse:
class Solution {
public List<String> topKFrequent(String[] words, int k) {
if(words == null || words.length == 0 || k <= 0){
return new ArrayList<String>();
}
Map<String, Integer> map = new HashMap<>();
PriorityQueue<Map.Entry<String,Integer>> pq = new PriorityQueue<>((e1, e2) -> compareElement(e1,e2));
for(String str : words){
map.put(str, map.getOrDefault(str, 0)+1);
}
for(Map.Entry<String,Integer> entry : map.entrySet()){
pq.add(entry);
if(pq.size() > k){
pq.poll();
}
}
List<String> res = new ArrayList<>();
while(!pq.isEmpty()){
res.add(pq.poll().getKey());
}
Collections.reverse(res);
return res;
} private int compareElement(Map.Entry<String,Integer> e1, Map.Entry<String,Integer> e2){
if(e1.getValue() - e2.getValue() != 0)
{return e1.getValue() - e2.getValue();}
else
{return e2.getKey().compareTo(e1.getKey());}
}
}
LeetCode - Top K Frequent Words的更多相关文章
- [LeetCode] Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [LeetCode] 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 "Top K Frequent Elements"
A typical solution is heap based - "top K". Complexity is O(nlgk). typedef pair<int, un ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
随机推荐
- day05 数据类型
一.整形int 基本使用: 1,用途:记录年龄\等级\各种号码 2定义方式: age=18 age =int(18) x =int(‘123’)#只能将纯数字的字符串转换成整形 print(t ...
- EF-记录程序自动生成并执行的sql语句日志
在EntityFramework的CodeFirst模式中,我们想将程序自动生成的sql语句和执行过程记录到日志中,方便以后查看和分析. 在EF的6.x版本中,在DbContext中有一个Databa ...
- java 2D图形绘制
package jisuan; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; imp ...
- UFT测试本地应用程序登陆小实例(描述性编程)
Dim username,password Dim casecount,i Dim currentid DataTable.ImportSheet ,"Action1" casec ...
- table 奇行偶行
jquery 方法: $("tr:odd").//偶行 $("tr:even") //奇行
- python的条件判断
条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...
- 牛客oi测试赛 二 B 路径数量
题目描述 给出一个 n * n 的邻接矩阵A. A是一个01矩阵 . A[i][j]=1表示i号点和j号点之间有长度为1的边直接相连. 求出从 1 号点 到 n 号点长度为k的路径的数目. 输入描述: ...
- day 51
一 window对象 window 对象表示一个浏览器窗口. 在客户端 JavaScript 中,Window 对象是全局对象,所有的表达式都在当前的环境中计算.也就是说,要引用当前窗口根本不需要特殊 ...
- 1)jquery validate 远程验证remote,自定义验证 , 手机号验证 2)bootstrap validate 远程remote验证的方法.
1)jquery validate 远程验证remote,自定义验证 1-1: js <script src="YYFramework/Public/js/jquery-3.1.1. ...
- usort 函数
function getNameFromNumber($num){ // Used to figure out what the Excel column name would be for a gi ...