当前位置:网站首页>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;
}
}
边栏推荐
- 『期末复习』16/32位微处理器(8086)基本寄存器
- Causal AI, a new paradigm for industrial upgrading of the next generation of credible AI?
- Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
- When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
- Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
- 金仓KFS数据双向同步场景部署
- sklearn数据预处理
- GFS分布式文件系统(光是遇见已经很美好了)
- 因子选股-打分模型
- Deep dive kotlin synergy (20): build flow
猜你喜欢

Busycal latest Chinese version
![[fxcg] market analysis today](/img/ac/294368e3496a5b808b38833053ee81.jpg)
[fxcg] market analysis today

Five elements of user experience

Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积

CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes

vulnhub HA: Natraj

The latest activation free version of Omni toolbox

2022deepbrainchain biweekly report no. 104 (01.16-02.15)

js实现在可视区内,文字图片动画效果

300+ documents! This article explains the latest progress of multimodal learning based on transformer
随机推荐
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
Social phobia of contemporary young people (II)
What is the correct way to compare ntext columns with constant values- What's the right way to compare an NTEXT column with a constant value?
vulnhub HA: Natraj
MySQL field userid comma separated save by userid query
[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification
[graduation season · aggressive technology Er] Confessions of workers
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
The longest subarray length with a positive product of 1567 recorded by leecode
PostgreSQL database high availability Patroni source code learning - etcd class
arthas watch 抓取入参的某个字段/属性
sklearn数据预处理
解决bp中文乱码
[untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
Classes in TS
JS realizes the animation effect of text and pictures in the visual area
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 =
Taking two column waterfall flow as an example, how should we build an array of each column
JS native common knowledge
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended