LeetCode-215. Kth Largest Element in an Array [C++][Java]

LeetCode-215. Kth Largest Element in an Arrayicon-default.png?t=M0H8https://leetcode.com/problems/kth-largest-element-in-an-array/

题目描述

Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: nums = [3,2,1,5,6,4], k = 2
Output: 5

Example 2:

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4

Constraints:

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

解题思路

【C++】

快排partition

1. 循环

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        if (k <= 0 || k > nums.size()) {return -1;}
        k--;
        int l = 0, h = nums.size() - 1, idx = partition(nums, l, h);
        while(idx != k ) { 
            if(idx < k) {
                idx = partition(nums, idx+1, h);
            } else {
                idx = partition(nums, l, idx-1);
            }
        }
        return nums[k];
    }

    int partition(vector<int>& nums, int l, int h) {
        int pivot = nums[l];
        while(l < h) {
            while(pivot>=nums[h] && l<h) h--;
            nums[l] = nums[h];
            while(pivot<=nums[l] && l<h) l++;
            nums[h] = nums[l];
        }
        nums[l] = pivot;
        return l;
    }
};

2. 递归

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        if (k <= 0 || k > nums.size()) {return -1;}
        partition(nums, 0, nums.size() - 1, --k);
        return nums[k];
    }

    void partition(vector<int>& nums, int l, int h, int k) {
        int pivot = nums[l], low = l, high = h;
        while(l < h) {
            while(pivot>=nums[h] && l<h) h--;
            nums[l] = nums[h];
            while(pivot<=nums[l] && l<h) l++;
            nums[h] = nums[l];
        }
        nums[l] = pivot;
        if (k == l) {return;}
        else if (l > k) {partition(nums, low, l - 1, k);}
        else {partition(nums, l + 1, high, k);}
    }
};

【Java】

class Solution {
    public int findKthLargest(int[] nums, int k) {
        if (nums.length == 0 || nums.length < k) { return -1;}
        partition(nums, 0, nums.length - 1, --k);
        return nums[k];
    }

    void partition(int[] nums, int low, int high, int k) {
        int pivot = nums[low], l = low, h = high;
        while(l < h) {
            while(pivot >= nums[h] && l < h) h--;
            nums[l] = nums[h];
            while(pivot <= nums[l] && l < h) l++;
            nums[h] = nums[l];
        }
        nums[l] = pivot;
        if (l == k) {return;}
        else if (l > k) {partition(nums, low, l - 1, k);}
        else {partition(nums, l + 1, high, k);}
    }
}

参考文献

【1】使用快速排序方法求第K大的数_qq_42211773的博客-CSDN博客_求第k大的数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值