当前位置:网站首页>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
边栏推荐
- Flutter Widget : Flow
- Vulnhub's cereal
- LeetCode 0556.下一个更大元素 III - 4步讲完
- Socket TCP for network communication (I)
- laravel 时区问题timezone
- Introduction to the implementation principle of rxjs observable filter operator
- Qt OpenGL 旋转、平移、缩放
- Laravel time zone timezone
- 242. Effective letter heteronyms
- Dart: about Libraries
猜你喜欢

PHP導出word方法(一mht)

Groovy test class and JUnit test

vulnhub之Ripper

Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022

【附下载】密码获取工具LaZagne安装及使用

AOSP ~ NTP (Network Time Protocol)

Integer string int mutual conversion

vulnhub之momentum

QT OpenGL rotate, pan, zoom

Is BigDecimal safe to calculate the amount? Look at these five pits~~
随机推荐
Integer string int mutual conversion
Solution à la défaillance de l'installation d'Electron
Visual studio 2022 downloading and configuring opencv4.5.5
QT OpenGL rotate, pan, zoom
Dart: About zone
网络通讯之Socket-Tcp(一)
pragma-pack语法与使用
Optimize interface performance
Socket TCP for network communication (I)
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
shardingSphere分库分表<3>
XML (DTD, XML parsing, XML modeling)
Raven2 of vulnhub
C language improvement article (wchar_t) character type
Vulnhub narak
temp
Wechat applet - basic content
安裝electron失敗的解决辦法
Ripper of vulnhub