栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

(top200)215. 数组中的第K个最大元素(待总结整理)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(top200)215. 数组中的第K个最大元素(待总结整理)

topk问题

https://leetcode-cn.com/problems/kth-largest-element-in-an-array/solution/zi-jie-ti-ku-215-zhong-deng-shu-zu-zhong-kjbd/
剑指offer40




(非递归的)

class Solution {
public:
    int findKthLargest(vector& nums, int k) {
        int n=nums.size();
        int le=0, ri=n-1;
        while(true){  
            //快排框架
            int i=le, j=ri;
            int idx = rand() % (ri - le +1) + le;//随机选择pivot
            swap(nums[le], nums[idx]);
            while(i=nums[le]) --j;
                while(in-k) ri=i-1;//将查找范围放在该位置左侧
            else le=i+1;//将查找范围放在该位置右侧
        }
    }
};

作者:gcsxdu
链接:https://leetcode-cn.com/problems/kth-largest-element-in-an-array/solution/215-shu-zu-zhong-de-di-kge-zui-da-yuan-s-vhwd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


    int findKthLargest(vector& nums, int k) {
        priority_queue, greater> pq;
        for (auto n : nums) {
            if (pq.size() == k && pq.top() >= n) continue;
            if (pq.size() == k) {
                pq.pop();
            }
            pq.push(n);
        }
        return pq.top();
    }

作者:ikaruga
链接:https://leetcode-cn.com/problems/kth-largest-element-in-an-array/solution/215-by-ikaruga/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/667630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号