当前位置:网站首页>2022-02-13 (347. Top k high frequency elements)
2022-02-13 (347. Top k high frequency elements)
2022-07-03 04:18:00 【TickTick123】
My most simple idea is to traverse , Get values and times map, Right again map Sort the heap by the number of times in , Or you can line up quickly k Large number . The official answer is the same as I thought .
But I see the following bucket sorting , In fact, it is the idea of exchanging space for time
class Solution {
public int[] topKFrequent(int[] nums, int k) {
int[] result=new int[k];
HashMap<Integer,Integer> map=new HashMap();
for(int num:nums){
if(map.containsKey(num)){
map.put(num,map.get(num)+1);
}else{
map.put(num,1);
}
}
// Bucket sort
// Take the frequency as the array subscript , For a set of numbers with different frequencies , Store the corresponding array subscript
List<Integer>[] list=new List[nums.length+1];
for(int key:map.keySet()){
int i=map.get(key);
if(list[i]==null){
list[i]=new ArrayList();
}
list[i].add(key);
}
int count=0;
for(int i=list.length-1;i>=0&&count<k;i--){
if(list[i]==null)continue;
for(Integer n:list[i]){
result[count++]=n;
}
}
return result;
}
}
边栏推荐
- DAPP for getting started with eth
- JS realizes the animation effect of text and pictures in the visual area
- Bisher - based on SSM pet adoption center
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- 树莓派如何连接WiFi
- JS realizes lazy loading of pictures
- The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
- redis 持久化原理
- Idea shortcut keys
- How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
猜你喜欢
Introduction of pointer variables in function parameters
JS realizes the animation effect of text and pictures in the visual area
Supervised pre training! Another exploration of text generation!
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching
【刷题篇】 找出第 K 小的数对距离
使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭
Which code editor is easy to use? Code editing software recommendation
[NLP]—sparse neural network最新工作简述
Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
随机推荐
[fairseq] error: typeerror:_ broadcast_ coalesced(): incompatible function arguments
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
[home push IMessage] software installation virtual host rental tothebuddy delay
[brush questions] find the number pair distance with the smallest K
深潜Kotlin协程(十九):Flow 概述
JS native common knowledge
Redraw and reflow
Basic types of data in TS
CVPR 2022 | Dalian Technology propose un cadre d'éclairage auto - étalonné pour l'amélioration de l'image de faible luminosité de la scène réelle
Appium automated testing framework
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
Mutex and rwmutex in golang
MongoDB 慢查询语句优化分析策略
会员积分商城系统的功能介绍
Daily question - ugly number
MySQL create table
Mila、渥太华大学 | 用SE(3)不变去噪距离匹配进行分子几何预训练
[set theory] set concept and relationship (set represents | number set | set relationship | contains | equality | set relationship property)
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func main() { var a =
IPv6 foundation construction experiment