Leetcode 692. Top K Frequent Words
程序员文章站
2022-04-25 19:09:52
...
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
bool compare(pair<string, int>& a, pair<string, int>& b) {
if(a.second == b.second) {
return a.first < b.first;
}
return a.second > b.second;
}
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
vector<string> result;
unordered_map<string, int> stat;
for(string word: words) {
stat[word]++;
}
vector<pair<string, int>> values;
for(auto val: stat) {
values.push_back(val);
}
sort(values.begin(), values.end(), compare);
for(int i = 0; i < k; i++) {
result.push_back(values[i].first);
}
return result;
}
};
Reference
上一篇: Java 实现二叉搜索树的查找、插入、删除、遍历的图文详情
下一篇: 谈谈JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)_javascript技巧
推荐阅读
-
LeetCode刷题笔记(Top K Frequent Elements)
-
TOP K frequent-elements
-
C++实现LeetCode(692.前K个高频词)
-
LeetCode 692. Top K Frequent Words
-
Leetcode 692. Top K Frequent Words
-
692. Top K Frequent Words
-
LeetCode 347: 前 K 个高频元素 Top K Frequent Elements
-
Top K Frequent Elements
-
(Java)leetcode-347 Top K Frequent Elements
-
[Leetcode] Top K Frequent Elements