组中第k大的元素"/>
LeetCode【215】数组中第k大的元素
题目:
思路:
代码:
public int findKthLargest(int[] nums, int k) {PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o1 - o2);for (int i = 0; i < nums.length; i++) {if (queue.size() >= k) {if (queue.peek() < nums[i]) {queue.poll();queue.add(nums[i]);}} else {queue.add(nums[i]);}}return queue.peek();}
更多推荐
LeetCode【215】数组中第k大的元素
发布评论