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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
小七在进步28 分钟前
string的使用(2)
c++
青少儿编程课堂1 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:折半搜索优化子集枚举(Meet in the Middle)解析
c++·python·算法·bfs·信息学竞赛
wo_jack3 小时前
C++ 入门笔记(十七):list 入门 —— 为什么 list 不能 sort?
数据结构·c++·面试·stl·list
文丑颜不良啊4 小时前
LeetCode2541~2560题目解析
java·leetcode
青少儿编程课堂4 小时前
蓝桥杯青少组图形化 + 全国青少年信息素养大赛 + 电子学会Scratch考级:校园连连看解析
c++·python·算法·bfs·信息学竞赛
草莓熊Lotso4 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
文丑颜不良啊18 小时前
LeetCode2481~2500题目解析
java·leetcode
David猪大卫1 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
HPtiger1 天前
C++ 入门笔记:从 C 到 C++ 的核心过渡(命名空间、输入输出、缺省参数、重载、引用、内联)
c语言·c++·学习
文丑颜不良啊1 天前
LeetCode2461~2480题目解析
java·leetcode