当前位置:网站首页>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
边栏推荐
- 023(【模板】最小生成树)(最小生成树)
- vulnhub之GeminiInc
- ArcGIS application (XXI) ArcMap method of deleting layer specified features
- MySQL time zone solution
- Dart: about Libraries
- (构造笔记)ADT与OOP
- win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
- STL tutorial 10 container commonalities and usage scenarios
- Solutions to the failure of installing electron
- (construction notes) ADT and OOP
猜你喜欢

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

MCDF Experiment 1

Unity3d learning notes 5 - create sub mesh

Vulnhub narak

vulnhub之pyexp

Socket TCP for network communication (I)

CGroup introduction

OpenGL 索引缓存对象EBO和线宽模式

Vulnhub pyexp

Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
随机推荐
MySQL time zone solution
previous permutation lintcode51
CGroup introduction
Qt OpenGL 旋转、平移、缩放
网络通讯之Socket-Tcp(一)
php 获取文件夹下面的文件列表和文件夹列表
vulnhub之Ripper
023(【模板】最小生成树)(最小生成树)
MCDF Experiment 1
Wechat applet - basic content
vulnhub之GeminiInc
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Qt OpenGL相机的使用
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Momentum of vulnhub
Interview experience in summer camp of Central South University in 2022
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
How to convert a numeric string to an integer
Unity3d learning notes 5 - create sub mesh
Vulnhub's Nagini