https://leetcode.com/problems/top-k-frequent-words/

Given a non-empty list of words, return the k most frequent elements.

Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.

Example 1:

Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.

Example 2:

Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.

Note:

  1. You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  2. Input words contain only lowercase letters.

Follow up:

  1. Try to solve it in O(n log k) time and O(n) extra space.

代码:

class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
vector<string> res(k);
unordered_map<string, int> freq;
auto cmp = [](pair<string, int>& a, pair<string, int>& b) {
return a.second > b.second || (a.second == b.second && a.first < b.first);
};
priority_queue<pair<string, int>, vector<pair<string, int>>, decltype(cmp) > q(cmp);
for (auto word : words) ++freq[word];
for (auto f : freq) {
q.push(f);
if (q.size() > k) q.pop();
}
for (int i = res.size() - 1; i >= 0; --i) {
res[i] = q.top().first; q.pop();
}
return res;
}
};

  priority_queue 自定义排序 get    priority_queue 正常按照第一项从大到小 然后第二项从大到小就不符合题意需要第二项按从小到大 所以自定义

#Leetcode# 692. Top K Frequent Words的更多相关文章

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

  2. [leetcode]692. Top K Frequent Words频率最高的前K个单词

    这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...

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

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

  4. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

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

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

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

  8. [LC] 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  9. 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

随机推荐

  1. 2017-2018-1 20155220 《信息安全系统设计基础》课下实践——实现mypwd

    学习pwd命令 输入pwd命令 于是man 1 pwd查看pwd详细 然后查看pwd实现需要的系统调用man -k; grep 在这发现了一个功能相同的内核函数getcwd 到这步就很简单了,先查看这 ...

  2. v$pwfile_user表

    SQL> select * from v$pwfile_users;select * from v$pwfile_users; USERNAME SYSDB SYSOP SYSAS------- ...

  3. MFC 消息映射、分派和传递

    几个重要的结构体: struct AFX_MSGMAP { AFX_MSGMAP* pBaseMessageMap; AFX_MSGMAP_ENTRY* lpEntries; } struct AFX ...

  4. HBase数据结构(读书笔记 )

    背景: 最近在做一些跟大数据相关的东西,涉及到数据的存储和分析,考虑各个方面,选择使用HBase进行存储,使用原生Java API进行数据分析,之后会陆续写一系列来说明最近做的东西,给像我这样未曾涉及 ...

  5. virtualBox linux操作系统centos 挂载光盘

    virtualBox虚拟机linux centos 挂载光盘 第一步: 放入光盘 第二步: 挂载光驱 (/dev/cdrom) 1) 创建挂载点 mkdir /mnt/media 2) 挂载 moun ...

  6. Centos6.8操作防火墙

    设置防火墙iptables开放3306接口 在/etc/sysconfig下面有个iptables文件,在控制台输入命令 iptables -I INPUT -p tcp --dport 3306 - ...

  7. 3星|《规避政治风险:全球化企业必修课》:中国将赢得5G竞争

    规避政治风险:全球化企业必修课(<哈佛商业评论>增刊) <哈佛商业评论>的两篇文章+<财经>的1篇文章.把<财经>的文章放到增刊中,好像是第一次,我觉得 ...

  8. GitHub笔记(二)——远程仓库的操作

    二 远程仓库 1 创建联系 第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一 ...

  9. 如何通过阿里云APP进行域名备案?阿里云备案流程需要多久?

    如何通过阿里云APP进行域名备案? 1.准备备案材料(很多初次使用阿里云APP进行备案的同学会问备案需要准备哪些资料,不二版本下面就给大家一一列举出来) 个人备案需要材料: ⑴<用户网站备案授权 ...

  10. Tesseract 4 自行构建支持双引擎的tessdata 文件

    Tesseract 4 版本具备两种识别引擎:新的基于LSTM(神经网络)引擎与传统引擎.通过在初始化时设定不同的EngineMode启动. OCR Engine modes: 0 Legacy en ...