题目:
https://leetcode-cn.com/problems/kth-largest-element-in-an-array/
public class _215_FindKthLargest { public int findKthLargest(int[] nums, int k) { int len = nums.length; return quickSelect(nums, 0, len - 1, k - 1); } public int quickSelect(int[] nums, int left, int right, int k) { int p = partition(nums, left, right); if (p == k) { return nums[p]; } else { return p > k ? quickSelect(nums, left, p - 1, k) : quickSelect(nums, p + 1, right, k); } } public int partition(int[] nums, int low, int high) { int key = nums[low]; while (low < high) { while (low < high && nums[high] <= key) { high--; } swap(nums, low, high); while (low < high && nums[low] >= key) { low++; } swap(nums, low, high); } return low; } public void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }