Leetcode优先队列题目

以下为Datawhale Leetcode开源学习思路总结,以下代码均为Leetcode代码,但不一定是最优解,仅供参考学习。

优先队列参考链接c++优先队列(priority_queue)用法详解,若链接失效,自行百度用法。

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

第一种做法直接排序,然后按地址访问第K个即可,简单,不做解释;第二种就是使用优先队列,我们采用大顶队,然后弹出k-1次,最后位于大顶堆top的位置元素就是我们要的元素,代码如下:

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        vector<int> res;
        priority_queue<int, vector<int>, less<int>> q(nums.begin(), nums.end()); 

        while(--k) {
            q.pop();
        }
        return q.top();
    }
};

347. 前K个高频元素

本题通过mapkeyvalue结构存储数字及其频次,我们在将unordered_map元素输入至优先队列中时,不需要先创建一个vector<pair<.., ..>>,再拷贝至优先队列中,直接将map结构push进去即可,我们push的就是一个pair<.., ..>,那么题目没啥难度,结合Lambda表达式和function函数对象后,代码非常简单,如下:

class Solution {
    // class mycmp {
    // public:
    //     bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    //         return a.second > b.second;
    //     }
    // };
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        // unordered_map<int, int> mp;
        // for(auto x : nums) {
        //     mp[x]++;
        // }

        // vector<pair<int, int>> vec(mp.begin(), mp.end());
        // sort(vec.begin(), vec.end(), 
        //     [](pair<int, int> &a, pair<int, int> &b)->bool{
        //         return a.second > b.second;
        //     }
        // );
        // vector<int> res;
        // for(int i = 0; i < k; ++i) {
        //     res.push_back(vec[i].first);
        // }
        // return res;

        unordered_map<int, int> mp;
        for(auto x : nums) mp[x]++;

        // auto func = [](const pair<int, int> &a, const pair<int, int> &b)->bool{return a.second > b.second;};
        // priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(func)> q(func);
        priority_queue<pair<int, int>, vector<pair<int, int>>, function<bool(pair<int, int> &, pair<int, int>&)>> q(
            [](pair<int, int> &a, pair<int, int> &b)->bool {
                return a.second > b.second;
            }
        );

        for(auto x : mp) {
            q.push(x);
            if(q.size() > k)
                q.pop();
        }

        vector<int> res(k, 0);
        for(int i = k-1; i >= 0; --i) {
            res[i] = q.top().first;
            q.pop();
        }
        return res;
    }
};

451. 根据字符出现频率排序

本题也是将mappair<.., ..>结构加入到优先队列,这点与347题相同,然后我们按照频次排序,最后恢复字符串并返回,不做解释了,直接看代码:

class Solution {
public:
    string frequencySort(string s) {
        unordered_map<char, int> mp;
        for(auto x : s) {
            mp[x]++;
        }

        priority_queue<pair<char, int>, vector<pair<char, int>>, function<bool(pair<char, int> &, pair<char, int> &)>> q(
            [](pair<char, int> &a, pair<char, int> &b)->bool {
                return a.second < b.second;
            }
        );

        for(auto x : mp) {
            q.push(x);
        }
        string res = "";
        while(!q.empty()) {
            for(int i = 0; i < q.top().second; ++i)
                res += q.top().first;
            q.pop();
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值