简介
使用大顶堆 和快排实现
奇怪的是, 使用大顶堆还比快排慢.
code
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
std::priority_queue<int> big_heap; // 构造一个默认最大堆
for(auto it: nums) {
big_heap.push(it);
}
int num = 0;
while(k){
k--;
num = big_heap.top();
big_heap.pop();
}
return num;
}
};
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
}
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};