1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
var findKthLargest = function (nums, k) { return quickSort(nums, 0, nums.length - 1)
function quickSort(nums, low, high) { let p = _partition(nums, low, high) if (p === nums.length - k) return nums[p] else if (p < nums.length - k) return quickSort(nums, p + 1, high) else return quickSort(nums, low, p - 1) }
function _partition(nums,low,high){ let pivot = nums[low] while(low < high){ while(low < high && nums[high] >= pivot) high-- nums[low] = nums[high] while(low < high && nums[low] < pivot) low++ nums[high] = nums[low] } nums[low] = pivot return low } };
|