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

题目

思路

1. Using HashMap and PriorityQueue
2. Build a HashMap to get the word frequency
3. PriorityQueue (minHeap) - if the counts are same return higher lexicographically word first so that the lower remains in the queue
4. We add each entry to PQ. If the PQ size > k, then we pop the lowest value in the PQ.

[leetcode]347. Top K Frequent Elements K个最常见元素思路完全一致

代码

 /*
Time: O(nlogk)
Space: O(n)
*/ class Solution {
public List<String> topKFrequent(String[] words, int k) {
List<String> result = new LinkedList<>();
// freq map
Map<String, Integer> map = new HashMap<>();
for(String s: words){
map.put(s, map.containsKey(s) ? map.get(s) + 1 : 1);
}
// b.getKey().compareTo(a.getKey()) 处理了频率相同,按字典顺序排列的题意要求
PriorityQueue<Map.Entry<String, Integer>> minHeap = new PriorityQueue<>(
(a,b) -> a.getValue()==b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue()-b.getValue()
); for(Map.Entry<String, Integer> entry: map.entrySet())
{
minHeap.offer(entry);
if(minHeap.size()>k)
// 自定义从小到大的顺序保证了poll出来的是较小值,留在minHeap里的是较大值
minHeap.poll();
} while(!minHeap.isEmpty())
result.add(0, minHeap.poll().getKey()); return result;
}
}

[leetcode]692. Top K Frequent Words K个最常见单词的更多相关文章

  1. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  2. #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 ...

  3. [leetcode]692. Top K Frequent Words频率最高的前K个单词

    这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...

  4. [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 ...

  5. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  6. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  7. [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 ...

  8. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  9. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

随机推荐

  1. table布局与div布局

      DIV与TABLE本身并不存在什么优缺点,所谓web标准只是推荐的是正确的使用标签,好比说:DIV用于布局,而TABLE则本来就是转二维数据的.让TABLE做该做的事,并不是说页面里不出现TABL ...

  2. HTML一般标签

    <title>无标题文档</title> </head> <body bgcolor="#33CC33" background=" ...

  3. byobu session window split

    new session:  Ctrl + Shift + F2 window: F2 split: Shift/Ctrl + F2 move session: Alt + Up/Down window ...

  4. python import sklearn出错 "ImportError: DLL load failed: 找不到指定的模块。

    安装好sklearn模块后,import的时候出现了以下错误: 但是确实已经装好了,百思不得其解,网上查找之后发现,出现错误原因:安装包的来源问题,也可以理解为包版本兼容问题,有的包使用官方出版,有的 ...

  5. lcd 显示屏

    1.lcd 接口信号: VSYNC : 一帧新数据的开始信号 HSYNC :一行新数据的开始信号 VCLK   :像素的同步信号 VD[0:23]  :传递数据的信号线 2. LCD  的显示原理 ( ...

  6. linux下的arm汇编程序

    1.gnu 的编译环境搭建 解压编译工具,加入环境变量PATH 2.编译相关命令的使用 编译命令 arm-linux-gcc  -g -c -o led.o main.o led.c main.c / ...

  7. node+mysql简单注册

    1前台文件 <!doctype html> <html> <head> <meta charset="UTF-8" /> <t ...

  8. jquery 页面传值 汉字

    function getURLParameter(name) { return decodeURIComponent( (new RegExp('[?|&]' + name + '=' + ' ...

  9. Java将一个字符串的首位改为大写后边改为小写的实现,String

    Java将一个字符串的首位改为大写后边改为小写的实现,String 思路: 获取首字母, charAt(0) substring(0,1) 转成大写 toUpperCase() 转大写hellO=== ...

  10. Sublime Text使用中的一些心得

    Sublime Text3是每个web前端程序员的必备神器,其中有许多便利的功能及插件.下面列出一些在开发中比较实用的快捷操作,可以极大地提高代码的编写速度及效率. l  在文档中输入代码,即使忘记保 ...