题解 | #寻找第K大#

寻找第K大

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

主要思想: 利用PriorityQueue维护一个大小为K的有序的窗口,这个PriorityQueue是利用的堆排序。

import java.util.*;

public class Solution {
    public int findKth(int[] a, int n, int K) {
        // write code here
        PriorityQueue<Integer> pq = new PriorityQueue<>((num1, num2)-> num1 - num2);
        for(int i = 0; i < a.length; i++) {
            pq.offer(a[i]);
            if(pq.size() > K) {
                pq.remove();
            }
        }
        return pq.peek();
    }
}
全部评论

相关推荐

Rena1ssanc...:对的,要是面评没太烂,勤更新简历等捞就行了,腾讯可以无限复活
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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