题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
class Solution {
public:
void swap(int &a,int & b){
int t=a;
a=b;
b=t;
}
void sort(vector<int> &a,int low,int high){
int i=low;
int j=high;
int key=a[low];
if(i>=j) return;
while(i!=j){
while(a[j]>=key&&j>i) j--;
swap(a[j], a[i]);
while(a[i]<key&&i<j) i++;
swap(a[i], a[j]);
}
sort(a,low,i-1);
sort(a,i+1,high);
}
int findKth(vector<int> a, int n, int K) {
// write code here
sort(a,0,n-1);
return a[n-K];
}
};</int></int>