leetcode692 Top K Frequent Words
思路:
堆。
实现:
#include <bits/stdc++.h>
using namespace std; class Solution
{
public:
inline bool cmp(pair<int, string> a, pair<int, string> b)
{
if (a.first > b.first) return true;
if (a.first == b.first && a.second < b.second) return true;
return false;
}
struct cmp1
{
bool operator()(const pair<int, string>& a, const pair<int, string>& b) const
{
if (a.first > b.first) return true;
if (a.first == b.first && a.second < b.second) return true;
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k)
{
unordered_map<string, int> mp;
for (auto it : words) mp[it]++;
priority_queue<pair<int, string>, vector<pair<int, string>>, cmp1> q;
int cnt = ;
for (auto it : mp)
{
pair<int, string> tmp(it.second, it.first);
if (cnt < k)
{
q.push(tmp);
}
else if (cmp(tmp, q.top()))
{
q.push(tmp);
q.pop();
}
cnt++;
}
vector<string> ret;
while (!q.empty())
{
ret.push_back(q.top().second); q.pop();
}
reverse(ret.begin(), ret.end());
return ret;
}
};
int main()
{
vector<string> v = {"i", "love", "leetcode", "i", "love", "coding"};
vector<string> ret = Solution().topKFrequent(v, );
for (auto it : ret)
{
cout << it << " ";
}
cout << endl;
return ;
}
leetcode692 Top K Frequent Words的更多相关文章
- [LeetCode] Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [LeetCode] Top K Frequent Words 前K个高频词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]692. Top K Frequent Words K个最常见单词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 最高频的K个单词 · Top K Frequent Words
[抄题]: 给一个单词列表,求出这个列表中出现频次最高的K个单词. [思维问题]: 以为已经放进pq里就不能改了.其实可以改,利用每次取出的都是顶上的最小值就行了.(性质) 不知道怎么处理k个之外的数 ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
随机推荐
- Ubuntu清理内存命令(效果不明显)
注意:最好不要在生产环境上使用!!! 1.检查内存使用情况 watch -n 3 free -m watch -n 3 cat /proc/meminfo 2.清理 #释放页缓存 echo 1 > ...
- Linux下查看硬盘UUID和修改硬盘UUID(转)
查看硬盘UUID: 1. ls -l /dev/disk/by-uuid 2. blkid /dev/sda5 修改硬盘UUID: 1.新建和改变分区的UUID sudo uuidgen | xarg ...
- MongoDB小结07 - update【$pop】
如果将数组看做队列,可以用$pop方法删除第一个或者最后一个元素 {$pop:{"key":-1}},{$pop:{"key":1}}
- openstack setup demo 前言
我们搭建一套三节点的openstanck集群.一个controller节点,两个compute节点.操作系统采用Centos7,操作系统版本信息如下. [root@controller01 ~]# c ...
- 移动端 js 实现图片上传 预览
方法一: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&q ...
- YII数据流程浅析
MVC就不解释,直接上代码分析数据流程: 数据库图: 模型部分介绍: <?php /* * 前两个方法必须写 * 继承自CActiveRecord类 这个类位于 \framework\db\ar ...
- antd 的 Table 遇到的 bug
1.报错情况 /* autoprefixer: off */ filter: progid\:DXImageTransform\.Microsoft\.Blur(PixelRadius\=1, Mak ...
- JavaScript全局属性/函数
JavaScript 全局属性和方法可用于创建Javascript对象. JavaScript 全局属性 属性 描述 Infinity 代表正的无穷大的数值. NaN 指示某个值是不是数字值. und ...
- 查看jvm常用命令
jinfo:可以输出并修改运行时的java 进程的opts. jps:与unix上的ps类似,用来显示本地的java进程,可以查看本地运行着几个java程序,并显示他们的进程号. jstat:一个极强 ...
- 在linux上处理base64加密和解密
http://snailwarrior.blog.51cto.com/680306/142472/ 2.从标准输入读取文件内容,base64编码并打印到标准输出 [root@localhost tes ...