当前位置:网站首页>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;
}
}
边栏推荐
- Interface in TS
- Leecode swipe questions and record LCP 18 breakfast combination
- RSRS指标择时及大小盘轮动
- JS realizes lazy loading of pictures
- 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
- Redraw and reflow
- "Designer universe" argument: Data Optimization in the design field is finally reflected in cost, safety and health | chinabrand.com org
- Application of I2C protocol of STM32F103 (read and write EEPROM)
- Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
- Social phobia of contemporary young people (II)
猜你喜欢

深潜Kotlin协程(十九):Flow 概述

因果AI,下一代可信AI的产业升级新范式?

【刷题篇】多数元素(超级水王问题)

MPLS setup experiment

用户体验五要素

Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended

JS realizes the animation effect of text and pictures in the visual area
![[home push IMessage] software installation virtual host rental tothebuddy delay](/img/e7/eb20a773e4b674962f856d179a3769.jpg)
[home push IMessage] software installation virtual host rental tothebuddy delay

JS realizes lazy loading of pictures

Is it better to speculate in the short term or the medium and long term? Comparative analysis of differences
随机推荐
xrandr修改分辨率與刷新率
金仓数据库KingbaseES 插件kdb_date_function
中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
Design and implementation of kubelet garbage collection mechanism to protect nodes from being preempted by containers image GC high threshold
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Arduino application development - LCD display GIF dynamic diagram
Sklearn data preprocessing
Supervised pre training! Another exploration of text generation!
[graduation season · aggressive technology Er] Confessions of workers
Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching
[Apple Photo Album push] IMessage group anchor local push
会员积分商城系统的功能介绍
Mongodb slow query optimization analysis strategy
The 10th China Cloud Computing Conference · China Station: looking forward to the trend of science and technology in the next decade
[Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
用户体验五要素
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
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
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 =
Database management tool, querious direct download