题解 | #寻找第K大#

寻找第K大

http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf

   public int findKth(int[] a, int n, int k) {
        PriorityQueue<Integer> q = new PriorityQueue<>();
        for(int x:a){
            q.offer(x);
            if(q.size()>k){
                q.poll();
            }
        }
        return q.peek();
    }

找大的元素用小根堆,找小的元素用大根堆
全部评论

相关推荐

评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客企业服务