当前位置:网站首页>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
边栏推荐
- OPenGL 基本知识(根据自己理解整理)
- (构造笔记)ADT与OOP
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- (construction notes) ADT and OOP
- Develop plug-ins for idea
- How to convert a numeric string to an integer
- Vulnhub's cereal
- (construction notes) learning experience of MIT reading
- Differences between MySQL Union and union all
- Laravel time zone timezone
猜你喜欢

During FTP login, the error "530 login incorrect.login failed" is reported

Vulnhub's Tomato (tomato)

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

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

vulnhub之Ripper

LeetCode 0556.下一个更大元素 III - 4步讲完

PHP export word method (one MHT)

vulnhub之tomato(西红柿)

Wrong arrangement (lottery, email)

Vulnhub's Nagini
随机推荐
ES6新特性
(构造笔记)ADT与OOP
Vulnhub narak
网络通讯之Socket-Tcp(一)
安裝electron失敗的解决辦法
pragma-pack语法与使用
CGroup introduction
[combinatorics] permutation and combination (example of permutation and combination)
Solve msvcp120d DLL and msvcr120d DLL missing
Vulnhub geminiinc V2
Shell: basic learning
网上炒股开户安不安全?谁给回答一下
(构造笔记)GRASP学习心得
[combinatorics] permutation and combination (summary of permutation and combination content | selection problem | set permutation | set combination)
Is BigDecimal safe to calculate the amount? Look at these five pits~~
(construction notes) ADT and OOP
Vulnhub's presidential
023 ([template] minimum spanning tree) (minimum spanning tree)
836. Merge sets (day 63) and search sets
242. Effective letter heteronyms