上善若水 大盈若冲
LeetCode link
solution
12345678910111213141516171819
class Solution { public int findKthLargest(int[] nums, int k) { if (nums == null || k > nums.length) { return Integer.MIN_VALUE; } PriorityQueue<Integer> minHeap = new PriorityQueue<>(k); for (int num: nums) { if (minHeap.size() < k) { minHeap.add(num); } else { if (num >= minHeap.peek()) { minHeap.poll(); minHeap.add(num); } } } return minHeap.peek(); }}