题解 | #寻找第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);
        }
    }
}
全部评论

相关推荐

06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
ohs的小木屋:比不少实习待遇高了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务