Leet_Code_215_找第k大的数

public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int num : nums) {
            if (queue.size() < k) {
                queue.add(num);
            } else if (queue.peek() < num){
                queue.poll();
                queue.add(num);
            }
        }
        return queue.poll();
    }

 

posted on 2021-03-17 07:38  MaXianZhe  阅读(39)  评论(0编辑  收藏  举报

导航