题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
快速排序+从打到小
class Solution {
public:
int findKth(vector<int> a, int n, int K) {
// write code here
return QuickSort(a, 0, n - 1, K);
}
int QuickSort(vector<int> a, int start, int end, int k)
{
int pivot = a[start];
int low = start;
int high = end;
int res = 0;
while(low < high)
{
while(low < high && a[high] <= pivot)
high--;
a[low] = a[high];
while(low < high && a[low] >= pivot)
low++;
a[high] = a[low];
}
a[low] = pivot;
if(low + 1 == k)
return a[low];
else if(low + 1 > k)
res = QuickSort(a, start, low - 1, k);
else
res = QuickSort(a, low + 1, end, k);
return res;
}
};
科大讯飞公司氛围 474人发布