给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。

    请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

    示例 1:

    输入: [3,2,1,5,6,4] 和 k = 2
    输出: 5
    示例 2:

    输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
    输出: 4

    提示:

    1 <= k <= nums.length <= 104
    -104 <= nums[i] <= 104


    1. class Solution {
    2. int[] nums;
    3. public int findKthLargest(int[] nums, int k) {
    4. this.nums = nums;
    5. int n = nums.length;
    6. return quick_sort(nums, 0, n - 1, n - k + 1);
    7. }
    8. public int quick_sort(int[] nums, int l, int r, int k){
    9. if (l >= r) return nums[l];
    10. int x = nums[l + r >> 1];
    11. int i = l - 1, j = r + 1;
    12. while (i < j) {
    13. do i ++ ; while (nums[i] < x);
    14. do j -- ; while (nums[j] > x);
    15. if (i < j) {
    16. int tem = nums[i];
    17. nums[i] = nums[j];
    18. nums[j] = tem;
    19. }
    20. }
    21. if (j - l + 1 >= k) return quick_sort(nums, l, j, k);
    22. else return quick_sort(nums, j + 1, r, k - (j - l + 1));
    23. }
    24. }