思路:

堆。
实现:

 #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的更多相关文章

  1. [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 ...

  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 ...

  3. [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 ...

  4. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  5. [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 ...

  6. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  7. 最高频的K个单词 · Top K Frequent Words

    [抄题]: 给一个单词列表,求出这个列表中出现频次最高的K个单词. [思维问题]: 以为已经放进pq里就不能改了.其实可以改,利用每次取出的都是顶上的最小值就行了.(性质) 不知道怎么处理k个之外的数 ...

  8. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

  9. 347. Top K Frequent Elements (sort map)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

随机推荐

  1. symfony could not load type 'datetime'

    当用curd生成控制器后,当修改的时候,会有这个提示,解决方法 在orm中通过事务的方式填充时间,然后把生成的form中的文件的时间段去掉 $builder ->add('title') -&g ...

  2. UVa 340 Master-Mind Hints(猜数字游戏的提示)

    题意  猜数字游戏  统计猜的数字有多少个数字位置正确  有多少个数字在答案中出现可是位置不对  每一个字符仅仅能匹配一次 直接匹配每位数 #include<cstdio> #includ ...

  3. Smart Home DIY 计划

    工作了这么长时间了,感觉自己眼下的工作内容非常不利于技术水平的提升,对此状况,我心里深感不踏实.因此,我决定利用下班时间.边学习边做,做一套真正可用的智能家居系统,首先部署到自己居住的房间. 对此智能 ...

  4. MySQl 子查询,左右连接,多表连接学习笔记

    1.子查询是指在还有一个查询语句中的SELECT子句.   例句:   SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);   当中, ...

  5. 操作JSON对象

    JSON类型对象,最简单了,就是键值对,key:value.key:value.一直不停地key:value下去,层层嵌套,理论上多少层都可以,只要你喜欢. 可是,每次应用JSON,我都心烦意乱,甚至 ...

  6. 编译自己的gcc

    1 编译gcc需要的依赖 gmp mpfr mpc isl binutils 将它们都安装在同一个目录下即可. 2 --disable-nls 将native language support关掉,只 ...

  7. ios25---图片拉伸

    控制器: // // ViewController.m // 12-图片的拉伸问题 // // Created by xiaomage on 15/12/30. // Copyright © 2015 ...

  8. 第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)

    Leetcode363 思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数. 先求出 ...

  9. bzoj4031 [HEOI2015]小Z的房间——矩阵树定理

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4031 矩阵树定理的模板题(第一次的矩阵树定理~): 有点细节,放在注释里了. 代码如下: # ...

  10. urllib2.urlopen超时未设置导致程序卡死

    没有设置timeout参数,结果在网络环境不好的情况下,时常出现read()方法没有任何反应的问题,程序卡死在read()方法里,搞了大半天,才找到问题,给urlopen加上timeout就ok了,设 ...