题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
import java.util.*;
public class Solution {
public int findKth(int[] a, int n, int K) {
// write code here
int b = quickSort(a, 0, a.length-1, K);
System.out.println(Arrays.toString(a));
return b;
}
public int quickSort(int[] a, int low, int high, int K){
int pivot = a[low];
int l=low, r=high;
while(l<r){
while(l<r&&a[r]>=pivot) r--;
a[l] = a[r];
while(l<r&&a[l]<=pivot) l++;
a[r] = a[l];
}
a[l] = pivot;
if(l == a.length-K){
return a[l];
} else if (l < a.length-K){
return quickSort(a, l+1, high, K);
} else {
return quickSort(a, low, l-1,K);
}
}
} 

查看20道真题和解析