Kth Largest Element in an Array

来源:互联网 发布:linux yum安装snmp 编辑:程序博客网 时间:2024/05/23 22:22

这道题我们先用heap做了,之后再用排序的方法处理。

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


0 0
原创粉丝点击