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. [UE4]Axis Mappings轴映射和动作映射Action Mappings的区别

    这里添加的映射只能在“玩家角色(Character)”的事件蓝图中使用. 从它们设置和蓝图使用界面就能看出他们的区别

  2. iframe引入百度地图显示企业位置

    步骤一:打开下面这个地址:http://api.map.baidu.com/lbsapi/creatmap/index.html     步骤二:定位中心点     在打开的页面左侧,输入企业的详细地 ...

  3. 被折腾的sql编程

  4. js将UTC时间转化为当地时区时间

    js将UTC时间转化为当地时区时间(UTC转GMT)   我们在进行网站开发的时候有可能会涉及到国外的用户或者用户身在国外,这时就会存在时差问题,比如说我们在中国的时间是08:00,但是此时韩国的时间 ...

  5. 激活函数sigmoid、tanh、relu、Swish

    激活函数的作用主要是引入非线性因素,解决线性模型表达能力不足的缺陷 sigmoid函数可以从图像中看出,当x向两端走的时候,y值越来越接近1和-1,这种现象称为饱和,饱和意味着当x=100和x=100 ...

  6. 对 Spring 的核心(AOP 和 IOC)的理解(大白话)

    Spring 首先它是一个开源而轻量级的框架.其核心容器的主要组件是Bean工厂(BeanFactory).Bean工厂使用控制反转(IOC)模式来降低程序代码之间的耦合度,并提供了面向切面编程(AO ...

  7. [Dart] Flutter 上传文件

    /** * 请求响应数据 */ class MsgResponse { int code; // 状态代码,0 表示没有错误 Object data; // 数据内容,一般为字符串 String er ...

  8. hadoop分布式快速搭建

    hadoop分布式快速搭建 1.配置主节点与从节点的ssh互信:[其中在主从节点主机的/etc/hosts文件中需绑定主机名ip间的映射关系; 如,192.168.1.113 node0 192.16 ...

  9. JS实现拖动效果

    有个问题就是该模块要使用定位,因为有left,top属性使用,绝对定位和相对定位都行,当然你也可使用margin-left,和margin-top这2个属性,替换left,top也是可以得 这样就不用 ...

  10. ActiveMQ无法启动

    解决办法:activemq无法启动,端口被占用 用netstat -an无法查出61616被哪个进程占用(实践证明,netstat -ano|findstr '61616'什么也没有找到) 经过排查和 ...