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 = [ ...
随机推荐
- JSP中操作Java Beans
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/beans.html: JavaBean是在编写Java时专门创建的Java类,根据JavaBean AP ...
- MongoDB小结02 - 配置、启动MongoDB
下载MongoDB 第一步:登上MongoDB官网,找到自己的适合的版本下载 第二步:解压(免安装),改名mongodb(举例命名,可以任个人喜好),放在你喜欢的位置(任喜好) 第三步:通过命令行: ...
- Java利用jacob实现文档格式转换
实现文档格式之间的转换,我使用的是jacob-1.7版本,需要jacob.jar来调用activex控件,本机需安装WPS/office,还需要jacob.jar以及jacob.dll 其中: ...
- excel 合并 单元格内容
刚刚有人问怎么合并单元格内容,正好excel 我也不会,顺便查查记录一下 1.假设有两个单元格如下: 单元格1 单元格2 2. 在一个空白单元格输入 =( 这代 ...
- 【Nginx】负载均衡-加权轮询策略剖析
转自:江南烟雨 本文介绍的是客户端请求在多个后端服务器之间的均衡,注意与客户端请求在多个nginx进程之间的均衡相区别. 如果Nginx是以反向代理的形式配置运行,那么对请求的实际处理需要转发到后端服 ...
- [React] Use Prop Collections with Render Props
Sometimes you have common use cases that require common props to be applied to certain elements. You ...
- [Java Sprint] Spring XML Configuration : Constructor Injection Demo
Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html Now le ...
- C#的DataGridView如何修改字体
在RowTemplate中可以修改字体和显示的格式(比如保留几位小数)
- Linux学习日志--文件搜索命令
开头总结: 学习了Linux中的文件搜索命令find和locate,系统搜索命令whereis 和which ,字符串搜索命令grep,find和locate的差别和使用方法格式,什么是path环境变 ...
- iOS中.pch文件怎样使用
pch 能够用来存储共享信息,比方设备屏幕的宽度,高度.版本等等 公用信息 Xcode 老版本号会自己主动为我们创建pch文件,新版本号開始不自己主动创建了.假设须要使用能够自己手动创建 waterm ...