题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
思路: 递归快排实现,寻找第k大的数
public class Solution {
public int findKth(int[] a, int n, int K) {
// write code here
return quikSort(a,0,n-1,K);
}
private static int quikSort(int[] a, int start, int end, int k) {
int temp=a[start];
int s=start,e=end;
while (s<e){
while (s<e&&temp>=a[e]) e--;
a[s]=a[e];
while (s<e&&temp<=a[s])s++;
a[e]=a[s];
}
a[s]=temp;
if (s==k-1){
return a[s];
}else if (s>k-1){
return quikSort(a,start,s-1,k);
}else if (s<k-1){
return quikSort(a,s+1,end,k);
}
return 0;
}
}


查看21道真题和解析