1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class Solution {
public:
int quick_sort(vector<int>& nums, int l, int r, int k) {
if (l == r) return nums[l];
int x = nums[l + r >> 1];
int i = l - 1, j = r + 1;
while (i < j) {
while (nums[++i] > x);
while (nums[--j] < x);
if (i < j) swap(nums[i], nums[j]);
}
if (k <= j) return quick_sort(nums, l, j, k);
return quick_sort(nums, j + 1, r, k);
}
int findKthLargest(vector<int>& nums, int k) {
return quick_sort(nums, 0, nums.size() - 1, k - 1);
}
};
|