当前位置:网站首页>347. Top k high frequency elements
347. Top k high frequency elements
2022-07-03 12:11:00 【zwanying】
Find the number of elements , Think of using map Recording .
front k High frequency elements : Sort .
map Cannot sort values , Replaceable map Key position , Or a map Turn into list To achieve .
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer,Integer> map = new HashMap();
// Record times
for(int i : nums){
if(map.containsKey(i)){
map.put(i,map.get(i)+1);
}else{
map.put(i,1);
}
}
// Sort
List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
int i =0;
nums = new int[k];
for (Map.Entry<Integer, Integer> e: list) {
nums[i++]=e.getKey();
if(i>=k){
break;
}
}
return nums;
}
}
Knowledge point :
map Cannot sort values directly , We need to convert to list, Use Collections.sort Sort .
The sorting time is either greater than , Or less than , Or equal to , There can be no ambiguity , Unable to determine the size .
o1>o2 It's in ascending order .
Other answers :
The most appropriate data structure : Priority queue .( Pile up , Perfect binary tree ), It can sort the data .
PriorityQueue<Map.Entry<Integer,Integer>> queue =
new PriorityQueue<>((o1,o2)-> o1.getValue-o2.getValue);
Knowledge point :
Priority queues use
Rewrite sorting method
offer == set
poll == get And delete
边栏推荐
- Visual studio 2022 downloading and configuring opencv4.5.5
- 242. Effective letter heteronyms
- DEJA_ Vu3d - cesium feature set 053 underground mode effect
- Shardingsphere sub database and sub table < 3 >
- vulnhub之pyexp
- vulnhub之tomato(西红柿)
- Concurrent programming - singleton
- regular expression
- 网络通讯之Socket-Tcp(一)
- [combinatorics] permutation and combination (example of permutation and combination)
猜你喜欢

vulnhub之tomato(西红柿)

Wrong arrangement (lottery, email)
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock

Ripper of vulnhub

Laravel time zone timezone

vulnhub之narak

Shutter: overview of shutter architecture (excerpt)

Raven2 of vulnhub

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

Solve msvcp120d DLL and msvcr120d DLL missing
随机推荐
【mysql专项】读锁和写锁
(construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
(构造笔记)ADT与OOP
Interview experience in summer camp of Central South University in 2022
php 获取文件夹下面的文件列表和文件夹列表
Fluent: Engine Architecture
Shell: basic learning
OpenGL 着色器使用
OpenGL 索引缓存对象EBO和线宽模式
pragma-pack语法与使用
Dart: about grpc (I)
Vulnhub narak
[combinatorics] permutation and combination (example of permutation and combination)
Swagger
安裝electron失敗的解决辦法
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
网上炒股开户安不安全?谁给回答一下
小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
ArcGIS application (XXI) ArcMap method of deleting layer specified features
typeScript