# LeetCode215. 数组中的第K个最大元素

输入: [3,2,1,5,6,4] 和 k = 2



输入: [3,2,3,1,2,4,5,5,6] 和 k = 4

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
return nums[nums.size()-k];
}
};

class Solution {
public:
int partition(vector<int> &nums,int left,int right){
int val = nums[right];
for(int i=left;i<right;i++){
if(nums[i]<val)
swap(nums[left++],nums[i]);
}
swap(nums[right],nums[left]);
return left;
}

int findKthLargest(vector<int>& nums, int k) {
int ans,left=0,right=nums.size()-1,pos=nums.size()-k;
while((ans=partition(nums,left,right))!=pos){
ans<pos?left=ans+1:right=ans-1;
}
return nums[pos];
}
};

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
make_heap(nums.begin(),nums.end(),less<int>());
int ans = 0;
for(int i=0;i<k;i++){
ans = nums.front();
pop_heap(nums.begin(),nums.end());
nums.pop_back();
}
return ans;
}
};

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int,vector<int>> q;  //显示说明内部是vector类型，也可以不加vector<int>
for(auto num:nums){
q.push(num);
}
int temp = 0;
for(int i=0;i<k;i++){
temp = q.top();
q.pop();
}
return temp;
}
};