Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

215. Kth Largest Element in an Array #166

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

public class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue que = new PriorityQueue( new Comparator() { public int compare(Integer o1, Integer o2) { if(o1 < o2) { return 1; } else if (o1 > o2) { return -1; } else { return 0; } } } );

    for(int i : nums) {
        que.offer(i);
    }

    for(int i = 0; i < k - 1; i++) {
        que.poll();
    }
    return que.poll();
}

} 这个网址很强 https://segmentfault.com/a/1190000003704825