215. Kth Largest Element in an Array
快排 双指针
Given an integer array nums
and an integer k
, return the k
th
largest element in the array.
Note that it is the k
th
largest element in the sorted order, not the k
th
distinct element.
Can you solve it without sorting?
Example 1:
Example 2:
Constraints:
1 <= k <= nums.length <= 10
5
-10
4
<= nums[i] <= 10
4
分析
注意第k大就是N-K+1小,N-K就是索引。所以后面直接返回nums[k]
partition函数每次返回左范围右边界。
注意判断条件是l<=r,对比二分l+1<r
Last updated
Was this helpful?