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

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷面很爱看电影:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 20:55
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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