692.前k个高频单词(堆&优先队列)

链接:692. 前K个高频单词 - 力扣(LeetCode)

题解:

push进入que中,不能只比较count,还需要比较字符串字典序列,所以要用node类型进行比较

cpp 复制代码
class Solution {
public:
    struct Node {
        Node(string s, int c) {
            word = s;
            count = c;
        }
        bool operator<(const Node& n) const {
            if (count > n.count) { // 约大的在下面
                return true;
            } else if (count == n.count &&
                       word < n.word) { // 字典许约小的在下面
                return true;
            }
            return false;
        }
        int count;
        string word;
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> table;
        for (auto& w : words) {
            ++table[w];
        }
        priority_queue<Node, vector<Node>> que;
        for (auto& e : table) {
            Node node(e.first, e.second);
            if (que.size() < k) {
                que.push(node);
            } else if (!que.empty() && node < que.top() ) {
                que.pop();
                que.push(node);
            }
            /*if (que.size() >= k) {
                Node node(e.first, e.second);
                que.push(node); // 先压入在弹出
                que.pop();

            } else {
                Node node(e.first, e.second);
                que.push(node);
            }*/
        }
        vector<string> result;
        result.resize(k);
        for (int i = k - 1; i >= 0; --i) {
            result[i] = que.top().word;
            que.pop();
        }
        /*result.reserve(k);
        while (!que.empty()) {
            result.push_back(que.top().word);
            que.pop();
        }*/
        //reverse(result.begin(), result.end());
        return result;
    }
};
cpp 复制代码
class Solution {
public:
    struct Node {
        Node(string s, int c) {
            word = s;
            count = c;
        }
        bool operator<(const Node& n) const {
            if (count > n.count) { // 约大的在下面
                return true;
            } else if (count == n.count &&
                       word < n.word) { // 字典许约小的在下面
                return true;
            }
            return false;
        }
        int count;
        string word;
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> table;
        for (auto& w : words) {
            ++table[w];
        }
        priority_queue<Node, vector<Node>> que;
        for (auto& e : table) {
            Node node(e.first, e.second);
            que.push(node);
            if (que.size() > k) {
                que.pop();
            }
            /*if (que.size() >= k) {
                Node node(e.first, e.second);
                que.push(node); // 先压入在弹出
                que.pop();

            } else {
                Node node(e.first, e.second);
                que.push(node);
            }*/
        }
        vector<string> result;
        result.resize(k);
        for (int i = k - 1; i >= 0; --i) {
            result[i] = que.top().word;
            que.pop();
        }
        /*result.reserve(k);
        while (!que.empty()) {
            result.push_back(que.top().word);
            que.pop();
        }*/
        //reverse(result.begin(), result.end());
        return result;
    }
};
cpp 复制代码
class Solution {
public:
    struct Node {
        Node(string s, int c) {
            word = s;
            count = c;
        }
        bool operator<(const Node& n) const {
            if (count > n.count) {// 约大的在下面
                return true;
            } else if (count == n.count && word < n.word) { // 字典许约小的在下面
                return true;
            }
            return false;
        }
        int count;
        string word;
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> table;
        for (auto& w : words) {
            ++table[w];
        }
        priority_queue<Node> que;
        for (auto& e : table) {
            if (que.size() >= k) {
                Node node(e.first, e.second);
                que.push(node); // 先压入在弹出
                que.pop();

            } else {
                Node node(e.first, e.second);
                que.push(node);
            }
        }
        vector<string> result;
        result.reserve(k);
        while (!que.empty()) {
            result.push_back(que.top().word);
            que.pop();
        }
        reverse(result.begin(), result.end());
        return result;
    }
};
cpp 复制代码
class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> cnt;
        for (auto& word : words) {
            cnt[word]++;
        }
        auto cmp = [](const pair<string, int>& a, const pair<string, int>& b) {
            return a.second == b.second ? a.first < b.first : a.second > b.second;
        };
        priority_queue<pair<string, int>, vector<pair<string, int>>, decltype(cmp)> que(cmp);
        for (auto& it : cnt) {
            que.emplace(it);
            if (que.size() > k) {
                que.pop();
            }
        }
        vector<string> ret(k);
        for (int i = k - 1; i >= 0; i--) {
            ret[i] = que.top().first;
            que.pop();
        }
        return ret;
    }
};

作者:力扣官方题解
链接:https://leetcode.cn/problems/top-k-frequent-words/solutions/785903/qian-kge-gao-pin-dan-ci-by-leetcode-solu-3qk0/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
程序猿编码30 分钟前
不用 PyTorch!从零手写 C++ 深度学习框架,自动微分、CUDA、分布式一把搞定
c++·深度学习·大模型推理·自动微分
real_metrix31 分钟前
【学习笔记】C/C++ 头文件自包含
c++·c·头文件·自包含
雪落漂泊33 分钟前
C++算法 滑动窗口篇(上)
开发语言·c++·算法
像枫一样34 分钟前
STM32串口轮询收发实战教程
arm开发·c++·stm32·单片机·嵌入式硬件·fpga开发·51单片机
Soneswy38 分钟前
C++基础篇——类与对象(上)
开发语言·c++
张小姐的猫40 分钟前
【MySQL】表的约束
android·linux·服务器·数据库·c++·mysql·adb
kiracrimson43 分钟前
STL简单引入:把数据结构做成模板
开发语言·数据结构·c++
kyle~1 小时前
数据结构 --- 无锁队列(Lock-Free Queue)
linux·数据结构·c++·多线程·并发
艾莉丝努力练剑1 小时前
【AI大模型接入SDK】ChatServer 路由规则绑定
网络·c++·人工智能·学习·ai·架构
王老师青少年编程1 小时前
2025年6月GESP真题及题解(C++一级): 选择题和判断题(题解)
c++·真题·gesp·答案·csp·一级·信奥赛