当前位置:网站首页>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;
}
}
边栏推荐
- Busycal latest Chinese version
- [set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
- [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
- [brush questions] find the number pair distance with the smallest K
- The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
- Xrandr modify resolution and refresh rate
- 以两列的瀑布流为例,我们应该怎么构建每一列的数组
- 金仓KFS数据双向同步场景部署
- Introduction of pointer variables in function parameters
- Reflection and planning of a sophomore majoring in electronic information engineering
猜你喜欢

Feature_selection

Database management tool, querious direct download

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

Mila, University of Ottawa | molecular geometry pre training with Se (3) invariant denoising distance matching

【刷题篇】接雨水(一维)

Basic MySQL operations

使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found

JS实现图片懒加载

Interaction free shell programming

"Final review" 16/32-bit microprocessor (8086) basic register
随机推荐
Interaction free shell programming
xrandr修改分辨率与刷新率
vulnhub HA: Natraj
[mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
[literature reading] sparse in deep learning: practicing and growth for effective information and training in NN
[Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
eth入门之简介
BMZCTF simple_ pop
GFS分布式文件系统(光是遇见已经很美好了)
Esp32 series (3): GPIO learning (take simple GPIO input and output, ADC, DAC as examples)
[set theory] set concept and relationship (set family | set family examples | multiple sets)
中移物联网OneOS与OneNET入选《2021年物联网示范项目名单》
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
金仓数据库KingbaseES 插件kdb_database_link
Bisher - based on SSM pet adoption center
Introduction to eth
Interface in TS
"Designer universe" argument: Data Optimization in the design field is finally reflected in cost, safety and health | chinabrand.com org
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments