【leetcode】215. Kth Largest Element in an Array

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

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

Example 2:

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

Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/

 

法:快排partition法

递归

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        return sort(nums, 0 , nums.size()-1, k);
    }
    int sort(vector<int>& nums, int left, int right, int k){
        int l = left, r = right-1, pivilot = right, key = nums[pivilot];
        while(l<=r){
            while(nums[l] >= key && l<=r){
                l++;
            }
            if(l<=r){
                nums[pivilot] = nums[l];
                pivilot = l++;
            }
            while(nums[r] <= key && l<=r){
                r--;
            }
            if(l<=r){
                nums[pivilot] = nums[r];
                pivilot = r--;
            }
        }
        nums[pivilot] = key;
        if(pivilot > k-1){
            return sort(nums, left, pivilot-1, k);
        }
        else if(pivilot < k-1){
            return sort(nums, pivilot+1, right, k);
        } 
        else return key;
    }
};

非递归

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        int l = 0, r=nums.size()-1;
        while(true){
            int p = partition(nums, l, r);
            if(p>k-1) r = p-1;
            else if(p<k-1) l = p+1;
            else return nums[p];
        }
    }
    int partition(vector<int>& nums, int left, int right){
        int l = left, r = right-1, pivilot = right, key = nums[pivilot];
        while(l<=r){
            while(nums[l] >= key && l<=r){
                l++;
            }
            if(l<=r){
                nums[pivilot] = nums[l];
                pivilot = l++;
            }
            while(nums[r] <= key && l<=r){
                r--;
            }
            if(l<=r){
                nums[pivilot] = nums[r];
                pivilot = r--;
            }
        }
        nums[pivilot] = key;
        return pivilot;
    }
};

法:堆

//TODO

法:

//TODO

六种解法:https://blog.csdn.net/zxzxzx0119/article/details/81509018

Leetcode215. 数组中第 k 大的数字(Kth Largest Element in an Array Leetcode - 215 Kth Largest Element in an Array (Medium) Input: [3,2,1,5,6,4] and k = 2 Output: 5 解法一:排序 public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.leng... 阅读详情

相关推荐

LeetCode215:Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2, return 5.Note:

2575

Kth Largest Element in an Array Leetcode #215 题解[C++]

题目来源 https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目理解 ind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted or...

Wayne_Mai的博客 426

leetcode 215: Kth Largest Element in an Array

leetcode 215: Kth Largest Element in an Array

西施豆腐渣 1万+

LeetCode 215:Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5.

哈哈的个人专栏 3868

Leetcode-215. Kth Largest Element in an Array

地址:https://leetcode.com/problems/kth-largest-element-in-an-array/ 描述: Find thekth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth...

Lost2013的博客 208

LeetCode 215. Kth Largest Element in an Array 题解

本题是经典的 Top K 问题。多种解法:排序、堆、快速选择快速选择可以达到线性时间注意第 k 大和索引的转换。

cannonjinx的博客 2981

Leetcode215. Kth Largest Element in an Array

大元素(其定义是从大到小排好序后位于第。个的数,即如果下标从。

数学、算法爱好者的博客 430

力扣215:数组中的第K个最大元素 (leetcode 215Kth Largest Element In An Array

力扣215:数组中的第K个最大元素 (leetcode 215Kth Largest Element In An Array

xxxnnn1991的博客 455

leetcode215. Kth Largest Element in an Array

题目描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 E...

中华小当家的博客 183

leetcode 215 Kth Largest Element in an array

目录leetcode215 Kth Largest Element in an array问题描述算法分析解法一:O(n)快速选择解法二:O(nlog(n)) 最小堆实现步骤代码实现快速查找最小堆 leetcode215 Kth Largest Element in an array 问题描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted ord

心安成长的码字书房 244

LeetCode215. Kth Largest Element in an Array

Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5...

Lightmare625 569

leetcode 215. Kth Largest Element in an Array

目录 一、问题分析 二、代码实现 1、排序 采用内置排序 采用冒泡排序 基数排序 2、堆 采用大顶堆 采用小顶堆 3、快速选择 递归版本 迭代版本 将输入数组随机化 采用中间下标的元素作为参照元素 https://leetcode.com/problems/kth-largest-element-in-an-array/ 返回数组中第k大的元素 一、问...

小景的博客 698

[leetcode] 215. Kth Largest Element in an Array 解题报告

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth dis

小榕流光的专栏 2543

leetcode215Kth Largest Element in an Array

写在最前面: 没有那个排序的话,这道题很简单。   leetcode215Kth Largest Element in an Array   Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kt...

ssjdoudou的博客 1万+

LeetCode 215. Kth Largest Element in an Array

欢迎访问原文所在博客:https://52heartz.top/articles/leetcode-215-kth-largest-element-in-an-array/ 解答1[Java]: import java.util.Arrays; class Solution { public int findKthLargest(int[] nums, int k) { i...

user2639的博客 161

Leetcode215. Kth Largest Element in an Array

找出无序数组第 k 大的数。

magic_jiayu的博客 217

leetcode笔记】Python实现215.Kth Largest Element in an Array

题目: 快速选择、堆排序:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 这里我们使用堆排序方法 这里讲解堆的定义特别清楚:https://blog.csdn.net/u013384984/article/details/79496052 下面是简化版: 堆的定义 大顶堆:每个节点的值都不大于...

Jack_Kuo的博客 888

leetcode215. Kth Largest Element in an Array

题目描述 在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 https://leetcode-cn.com/problems/kth-largest-element-in-an-array/ 解法1 时间复杂度:O(nlogn) 思路:使用快速排序,将数组排序,然后找到第k个最大的元素。这里需要复习一下快速一下快速排序的实...

weixin_30485799的博客 72

[leetcode] 215. Kth Largest Element in an Array @ python

原题 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Exa...

闲庭信步 498
上一篇: 【leetcode】125. Valid Palindrome
下一篇: 【leetcode】209. Minimum Size Subarray Sum
lemonade13
博客等级 码龄11年 11粉丝 253原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值