LeetCode 347. Top K Frequent Elements 前 K 个高频元素 (Java)
题目:
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
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 nis the array's size.
- It's guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique.
- You can return the answer in any order.
分析:
找出前K个高频元素,TopK的问题一般都可以利用堆,或者优先级队列来处理,统计好每个元素出现的顺序,加入到容量大小为k的堆中,即可得到答案。
程序:
class Solution {
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int num:nums){
int times = map.getOrDefault(num, 0);
map.put(num, times+1);
}
PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b)->{
return map.get(a) - map.get(b);
});
for(int num:map.keySet()){
minHeap.offer(num);
if(minHeap.size() > k)
minHeap.poll();
}
int[] res = new int[k];
for(int i = 0; i < res.length; ++i){
res[i] = minHeap.poll();
}
return res;
}
}
LeetCode 347. Top K Frequent Elements 前 K 个高频元素 (Java)的更多相关文章
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- [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] 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 ...
- 347 Top K Frequent Elements 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素.例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2].注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- [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 ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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. For example,Given [1,1,1,2 ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
随机推荐
- C++ 构造函数实战指南:默认构造、带参数构造、拷贝构造与移动构造
C++ 构造函数 构造函数是 C++ 中一种特殊的成员函数,当创建类对象时自动调用.它用于初始化对象的状态,例如为属性分配初始值.构造函数与类同名,且没有返回值类型. 构造函数类型 C++ 支持多种类 ...
- 涨姿势 | 一文读懂备受大厂青睐的ClickHouse高性能列存核心原理
简介: 本文尝试解读ClickHouse存储层的设计与实现,剖析它的性能奥妙 作者:和君 引言 ClickHouse是近年来备受关注的开源列式数据库,主要用于数据分析(OLAP)领域.目前国内各个大厂 ...
- 阿里集团业务驱动的升级 —— 聊一聊Dubbo 3.0 的演进思路
简介: 阿里云在 2020年底提出了"三位一体"理念,目标是希望将"自研技术"."开源项目"."商业产品"形成统一的技术 ...
- 深入解析 Dubbo 3.0 服务端暴露全流程
简介: 随着云原生时代的到来,Dubbo 3.0 的一个很重要的目标就是全面拥抱云原生.正因如此,Dubbo 3.0 为了能够更好的适配云原生,将原来的接口级服务发现机制演进为应用级服务发现机制. ...
- WPF 使用 ManipulationDemo 工具辅助调试设备触摸失效问题
本文将和大家介绍我所在的团队开源的 ManipulationDemo 工具.通过 ManipulationDemo 工具可以提升调试设备触摸失效的效率 此工具在 GitHub 上完全开源,请看 htt ...
- Windows 官方提供的触屏硬件延迟测量方法
本文记录微软 Windows 官方在 Windows Hardware Lab Kit 提供的触屏硬件延迟测量方法 Overview of measuring Touch Down Hardware ...
- spannerlib优雅的go异常处理
蹩脚的go 异常处理 一般写go的人,如果他不是写算法,正常写业务代码的话,可能都会为优雅的异常处理而烦恼,因为脑子抽筋的go设计者们,总是感觉语法糖是一种很低级的东西.但是在我们大多数公司的业务逻辑 ...
- Intel Pentium III 512MB内存 i815集显上安装Ubuntu Server 14.04
自己的御用奔腾III PC,接口齐全,准备安装Ubuntu Server 14.04 i386,继续发挥余热,物尽其用. 基本配置: CPU: Intel Pentium III 1000MHz, 2 ...
- Git实战技巧:恢复被强制push -f失踪的代码
前言 Git是一个易学难精的分布式版本控制系统,被我们码农常用于代码的管理.如果你还不了解Git,建议先通过廖雪峰的Git教程进行了解,再来看本文,因为本文以使用技巧为主,不会在基础名词上做过多解释. ...
- 五、Doris数据分布
在 Doris 中,数据都以表(Table)的形式进行逻辑上的描述 名词解释 数据分布:数据分布是将数据划分为子集, 按一定规则, 均衡地分布在不同节点上,以期最大限度地利用集群的并发性能 短查询:s ...