题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
class Solution { public: int findKth(vector<int> a, int n, int K) { // write code here //1.sort /*sort(a.begin(),a.end()); return a[n-K];*/ //2.大顶堆 /*priority_queue<int,vector<int>,greater<int>> small_queue; for(int i=0;i<K;i++){ small_queue.push(a[i]); } for(int i=K;i<n;i++){ if(a[i]>small_queue.top()){ small_queue.pop(); //一定要维护k大小,切记要弹出元素 small_queue.push(a[i]); } } return small_queue.top();*/ //2.冒泡的思路 for(int i=0;i<K;i++){ for(int j=n-1;j>i;j--){ if(a[j]>a[j-1]) swap(a[j],a[j-1]); } } return a[K-1]; //3.快排的思路 } };