Given a non-empty array of integers, return the k most frequent elements.

For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].

Note:

    • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
    • Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

题意:

给定数组,求其中出现频率最高的K个元素。

Solution: PriorityQueue

扫数组,将数组每个元素和其对应的出现频率存到Map里

扫Map,将Map里的元素放入maxHeap

从maxHeap里poll出K个元素即可

code

 class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> numFreqs = new HashMap(); for(int num : nums){
numFreqs.put(num, numFreqs.containsKey(num) ? numFreqs.get(num) + 1 : 1);
} PriorityQueue <Map.Entry<Integer, Integer>> maxHeap = new PriorityQueue<>((entry1, entry2) -> entry2.getValue() - entry1.getValue()); for(Map.Entry<Integer, Integer> entry : numFreqs.entrySet()){
maxHeap.add(entry);
} List<Integer> result = new ArrayList<>();
while(!maxHeap.isEmpty() && result.size() < k){
result.add(0, maxHeap.poll().getKey());
}
return result; }
}

[leetcode]347. Top K Frequent Elements 最高频的前K个元素的更多相关文章

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

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

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

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

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

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

  4. 347. Top K Frequent Elements (sort map)

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

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

  7. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

  8. LeetCode 【347. Top K Frequent Elements】

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  9. Leetcode 347. Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

随机推荐

  1. git修改用户名和邮箱

    用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. 1.查看用户名和地址 git config user.name git config us ...

  2. 第13章 TCP编程(3)_基于自定义协议的多进程模型

    5. 自定义协议编程 (1)自定义协议:MSG //自定义的协议(TLV:Type length Value) typedef struct{ //协议头部 ];//TLV中的T unsigned i ...

  3. ffmpeg同步

    1:ffmpeg解码流程 拆包,构建队列,解码,同步,显示 //计算视频Frame的显示时间//获取ptspts = 0;//decodec video frameavcodec_decode_vid ...

  4. HTML5进阶

    内容: 1.geolocation元素 2.video元素和audio元素 3.localStorage 4.WebWorker 5.WebSQL.IndexedDB 6.文件操作.文件拖拽新概念 7 ...

  5. ORM sqlachemy学习

    内容: 1.ORM介绍 2.SQLAlchemy介绍 3.SQLAlchemy内部处理 4.SQLAlchemy使用 参考: http://www.cnblogs.com/wupeiqi/articl ...

  6. uva-10562-二叉树

    题意: Homer教授被报道失踪了,我们怀疑这和他最近的研究有关,但是我们确实不知道他最近在研究什么. 侦探们试图侵入他的电脑,再几次失败后才意思到教授的智力超出他们很多............... ...

  7. HAproxy使用

    参考官网 安装HAproxy/ pull 官方镜像 本地安装:本地安装路径:/usr/local/haproxy/配置: 添加:/usr/local/haproxy/conf/haproxy.cfg添 ...

  8. 浏览器。浏览器对象检测、Chrome调试工具

    chrome浏览器的flash问题: 2017-12-26 chrome浏览器的flash有无法显示无法正常运行的问题时,解决方法如下: https://qzonestyle.gtimg.cn/qzo ...

  9. delphi const的用法

    unit RadKeygen; interface uses Classes,SysUtils,Windows; function fun1():string; implementation cons ...

  10. xe7 Unresolved external CSPIN.OBJ

    工程里打开使用了CSPIN控件的界面窗体,再编译就好了 [ilink32 Error] Error: Unresolved external 'Vcl::Controls::TControl::Set ...