当前位置: 首页 > news >正文

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

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

题解:

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

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; } };
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; } };
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; } };
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) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
http://www.jsqmd.com/news/1301784/

相关文章:

  • Box64终极指南:如何在ARM64设备上流畅运行x86_64 Linux程序
  • 瑞芯微RK3588S Android12 REE 指纹代码移植(1)--Kernel移植
  • 3个理由告诉你为什么BiliBili-UWP是Windows上最优秀的B站第三方客户端
  • 7 月总结:独立开发者从 0 到 1 的完整回顾——技术、产品与市场的三角平衡
  • VOC2028 安全帽佩戴检测数据集介绍、下载及YOLO/VOC/COCO训练格式转换
  • Python+Pyecharts实现动态销售数据可视化实战
  • 治愈系生活工具的设计系统:从颜色语义到组件规范
  • 第二阶段 15 · prefix / wildcard / fuzzy 模糊匹配
  • 从几个开源项目浅谈IOS视频流输出方案
  • 专科生论文写作工具对比:千笔与文途AI深度评测
  • 工具型PM发现Axure再熟练也画不出Agent的产品体验,开始重新学设计AI产品
  • RK平台SENSOR框架分析
  • 终极免费AI视频增强神器:3步将模糊视频无损升级到4K超高清
  • 内蕴时空正则化纲领:分形时间重构消解Navier–Stokes有限时间奇点
  • git多人开发流程
  • G-Helper终极指南:释放华硕笔记本隐藏性能的免费轻量级工具
  • 清华智谱开源发布ScaleCUA:可扩展的任务合成和在线强化学习 GUI Agent框架,9B模型刷新开源SOTA
  • 大语言模型选型指南:从技术原理到生产部署的实践路径
  • 祈盟游戏运营平台?业务范围、合作流程与适用团队说明
  • ComfyUI LLM Party终极指南:三步构建你的AI智能工作流
  • 1688货源对接抖店一件代发|密文下单+AI违规预检完整实操流程(2026合规标准版) - 电商分享
  • Python打字游戏开发:从零实现GUI打字速度练习工具
  • STM32定时器同步触发启动:硬件级精准时序控制实战指南
  • 无线动能开关|户外露天快递驿站雨棚照明免布线控制方案
  • STM32F407嵌入式开发实战:从Cortex-M4内核到以太网通信全解析
  • 告别游戏限制!3步解锁Wand-Enhancer专业功能完整体验
  • 全球DC/RF探针台市场前景调研分析及投资建议研究报告2026年版
  • GEZHI(格致):面向社科研究的智能体架构
  • 信息系统的五大组成要素
  • 观察五年,铝艺大门的几个真实现状