LeetCode.215 Kth Largest Element in an Array 找出k个最大的(*****经典必备题*****)

本文介绍了一种在未排序数组中查找第K大元素的方法,提供了四种不同的实现方案,包括使用PriorityQueue、List和数组排序等,以帮助理解不同数据结构的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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: 
You may assume k is always valid, 1 ≤ k ≤ array's length.


分析(利用PriorityQueue优先队列实现):

class Solution {
    public int findKthLargest(int[] nums, int k) {
        //给定数组,超出第k大的数
        //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了
        //PriorityQueue是一个基于优先级堆,此队列的头相对于指定的排序是最小的元素。
        
        //一个具有指定的初始容量。并且按自然排序。
        PriorityQueue<Integer> largeK=new PriorityQueue<Integer>();
        
        for(int n:nums){
            if(largeK.size()<k){
                largeK.add(n);
            }else{
                //集合中已经存在k个元素
                //判断是否小于最小值
                if(n>largeK.peek()){
                    largeK.poll();
                    largeK.add(n);
                }
            }
        }
        //最小的就是第k个结果
        return largeK.poll();
    }
}

分析2(简介版PriorityQueue):

class Solution {
    public int findKthLargest(int[] nums, int k) {
        //给定数组,超出第k大的数
        //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了
        //PriorityQueue是一个基于优先级堆,此队列的头相对于指定的排序是最小的元素。
        
        //一个具有指定的初始容量。并且按自然排序。设置k+1方便当size大于k时,剔除最前面的。
        PriorityQueue<Integer> largeK=new PriorityQueue<Integer>(k+1);
        
        for(int n:nums){
            //添加元素
            largeK.add(n);
            //如果长度大于k,则提出最小的,也就是最前面的
            if(largeK.size()>k){
                largeK.poll();
            }
        }
        //最小的就是第k个结果
        return largeK.poll();
    }
}

分析3(List实现-时间复杂度较高):

class Solution {
    public int findKthLargest(int[] nums, int k) {
        //给定数组,超出第k大的数
        //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了
        List<Integer> list=new ArrayList<Integer>();

        for(int i=0;i<nums.length;i++){
            if(list.size()<k){
                list.add(nums[i]);
            }else{
                //已经存在k个元素
                int min=0;
                //找出集合中最小的下标,和当前元素进行类比
                for(int j=1;j<list.size();j++){
                    if(list.get(j)<list.get(min)){
                        min=j;
                    }
                }
                 //类比
                if(nums[i]>list.get(min)){
                    //在最小值位置替换
                    list.set(min,nums[i]);
                }
            }
        }

        int res=0;
        for(int i=1;i<list.size();i++){
            if(list.get(i)<list.get(res)){
                res=i;
            }
        }
        return list.get(res);
    }
}

分析4(数组-排序实现,时间复杂度最高):

class Solution {
    public int findKthLargest(int[] nums, int k) {
        //给定数组,超出第k大的数
        //思路:利用stack实现,stack中始终保存着k个数据,每次和stack中最小的进行类比,比其大就替换,最后stack中最小的就是结果了
        //方法3:使用数组实现,每次都对数组进行排序
        int [] array=new int[k];

        for(int i=0;i<nums.length;i++){
            if(i<k){
                array[i]=nums[i];
            }else{
                //对数组排序
                Arrays.sort(array);
                if(nums[i]>array[0]){
                    array[0]=nums[i];
                }
            }
        }

        Arrays.sort(array);
        return array[0];
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值