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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
码匠许师傅6 小时前
【设计模式精讲】29.行为型模式总结与对比(Behavioral Patterns Summary)
c++·设计模式·uml
啦啦啦啦啦zzzz7 小时前
Logger的组装(c++20)
c++·算法·c++20
繁星蓝雨8 小时前
C++的设计与演进大纲(如何从C语言一步步成长为C++)
c语言·开发语言·c++·c++历史·c++演进
蒸蒸yyyyzwd8 小时前
机试学习笔记
c++·求职招聘
handler0110 小时前
【Linux】线程与虚拟内存的底层世界
linux·运维·服务器·c++·线程·虚拟地址空间·共享内存
郝学胜-神的一滴10 小时前
C++11 工程级应用 09:告别无谓拷贝,解锁高性能移动语义
开发语言·数据结构·c++·vscode·软件工程·visual studio
hetao173383712 小时前
校内场-提高组 ZYZSC-S-Round 4
c++·算法
stellanke12 小时前
Linux网络编程实战2:TCP Socket 基础与实践
linux·网络·c++
蒸蒸yyyyzwd13 小时前
机试和Redis学习笔记
c++·笔记·面试
tdtsmt13 小时前
2026年SMT贴片加工选型指南:天地通电子推荐参考
c++