• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

最常用的k个元素347.前K个频繁元素


Recommended Posts

用最大堆

//使用最大堆.将条目放入maxHeap,这样我们就可以总是以最大的频率轮询一个数字

公共类解决方案{

public list integer to PK frequency(int[]nums,int k) {

MapInteger,Integer map=new HashMap();

对于(int n: nums){

map.put(n,map.getOrDefault(n,0)1);

}

优先级队列图EntryInteger,整数最大堆=

new PriorityQueue((a,b)-(b . getvalue()-a . getvalue()));

对于(图EntryInteger,整数条目:图。条目集()){

最大堆。添加(条目);

}

list integer RES=new ArrayList();

while(res.size()k){

地图EntryInteger,整数条目=最大堆。poll();

资源添加(条目。getkey());

}

返回表示留数

}

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now