博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
215. Kth Largest Element in an Array
阅读量:4556 次
发布时间:2019-06-08

本文共 735 字,大约阅读时间需要 2 分钟。

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

For example,

Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 

You may assume k is always valid, 1 ≤ k ≤ array's length

题目含义:求给定的数组中,前k大个数

1     public int findKthLargest(int[] nums, int k) { 2 //        final int N = nums.length; 3 //        Arrays.sort(nums); 4 //        return nums[N - k]; 5         PriorityQueue
pq = new PriorityQueue<>(); 6 for (int i = 0; i < nums.length; i++) { 7 pq.offer(nums[i]); 8 if (pq.size() > k) pq.poll(); 9 }10 return pq.peek(); 11 }

 

转载于:https://www.cnblogs.com/wzj4858/p/7729725.html

你可能感兴趣的文章
刚学习的如何才能自信的拍美美的婚纱照呢(要结婚啦)
查看>>
M51文件注释
查看>>
关于临界资源访问互斥量的死锁问题
查看>>
django-view层
查看>>
异步加载JS的方法。
查看>>
golang-gorm框架支持mysql json类型
查看>>
【tool】白盒测试
查看>>
图论其一:图的存储
查看>>
20180923-WebService
查看>>
z变换
查看>>
Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数
查看>>
Spring基础2
查看>>
【灵异短篇】这个夜晚有点凉
查看>>
一点小问题
查看>>
pytest 10 skip跳过测试用例
查看>>
MVC身份验证及权限管理
查看>>
It was not possible to find any compatible framework version
查看>>
gulp与webpack的区别
查看>>
offset--BUG
查看>>
CSS选择器
查看>>