classSolution { public: intfindKthLargest(vector<int>& nums, int k){ int n = nums.size(); return quick_select(nums, 0, n - 1, n - k + 1); } intquick_select(vector<int>& nums, int l, int r, int k){ int randPos = l + rand() % (r - l + 1); swap(nums[l], nums[randPos]); int pivot = nums[l]; int i = l, j = r; while (i < j) { while (i < j && nums[j] >= pivot) --j; if (i < j) nums[i] = nums[j]; while (i < j && nums[i] < pivot) ++i; if (i < j) nums[j] = nums[i]; } nums[i] = pivot; int idx = i - l + 1; if (idx == k) return pivot; else { return idx < k ? quick_select(nums, i + 1, r, k - idx) : quick_select(nums, l, i - 1, k); } } };