LeetCode 215:数组中的第K个最大元素

这篇博客介绍了如何在给定整数数组中找到第k个最大的元素。提供了两种方法:一是利用最大和最小优先队列(Java实现),二是通过排序(冒泡和快速排序)。在优先队列方法中,使用Comparator进行比较,并通过poll()操作获取第k大的元素。而在排序方法中,直接返回排序后数组的第n-k个元素。这些算法对于寻找数组中的特定元素具有高效性。

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

链接

题目:
在这里插入图片描述

思路:

题目说不是第k个不同的元素,即重复的元素算多个,类似rank() 或者 Row_number(),而不是Dense_number()
使用最大优先队列(堆),poll() 第k次返回的即为第k大的数;
或者最小优先队列,poll()第n-k+1次即为第k大的数;
而使用数组,第k大的数,即nums数组中n-k索引的元素;

注意:Queue的方法,poll()会删除元素,peek()不会删除元素;

方法一:最大、最小优先队列

最大优先队列:
需要传入Comparator匿名内部类,重写compare方法;

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int n=nums.length;
        PriorityQueue<Integer> p=new PriorityQueue<>(new Comparator<Integer>(){
            @Override
            public int compare(Integer o1,Integer o2){
                return o2-o1; //  若o1 o2反过来则是从大到小排列
            }
        });
        for(int i=0;i<n;i++){
            p.add(nums[i]);
        }
        int r=0;
        for(int j=0;j<k;j++ ){
            r=p.poll();
        }
        return r;
    }
}

最小优先队列:

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int n=nums.length;
        PriorityQueue<Integer> p=new PriorityQueue<>();
        for(int i=0;i<n;i++){
            p.add(nums[i]);
        }
        int r=0;
        for(int j=0;j<n-k+1;j++ ){
            r=p.poll();
        }
        return r;
    }
}

方法二:排序

冒泡:(超时)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        // 冒泡
        for(int i=nums.length-1;i>0;i--){
            for(int j=0;j<i;j++){
                if(nums[j]>nums[j+1]){
                    int temp=nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=temp;
                }
            }
        }
        int n=nums.length;
        return nums[n-k];

    }
}

快排:

class Solution {
    public int findKthLargest(int[] nums, int k) {
        // 快排
        sort(nums);
        int n=nums.length;
        return nums[n-k];
    }

    void sort(int[] nums ){
        int lo=0;
        int hi=nums.length-1;
        sort(nums,lo,hi);
    }

    void sort(int[] nums,int lo,int hi){
        if(lo>=hi){
            return;
        }
        // 前序
        int partition=partition(nums,lo,hi);
        sort(nums,lo,partition-1);
        sort(nums,partition+1,hi);
    }

    int partition(int[] nums,int lo,int hi){
        int key=nums[lo];
        int left=lo;
        int right=hi+1;
        while(true){
            while(nums[--right]>key){
                if(right<=lo){
                    break;
                }
            }
            while(nums[++left]<key){
                if(left>=hi){
                    break;
                }
            }
            if(left>=right){
                break;
            }else{
                swap(nums,left,right);
            }
        }
        swap(nums,lo,right);
        return right;
    }

    void swap(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值