【Algorithm|Leetcode 692. Top K Frequent Words】文章作者:Tyan
博客:noahsnail.com|CSDN|简书
1. Description
文章图片
2. Solution
bool compare(pair& a, pair& b) {
if(a.second == b.second) {
return a.first < b.first;
}
return a.second > b.second;
}class Solution {
public:
vector topKFrequent(vector& words, int k) {
vector result;
unordered_map stat;
for(string word: words) {
stat[word]++;
}
vectorstring, 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
- https://leetcode.com/problems/top-k-frequent-words/description/
推荐阅读
- 数据结构与算法|【算法】力扣第 266场周赛
- leetcode|今天开始记录自己的力扣之路
- Python|Python 每日一练 二分查找 搜索旋转排序数组 详解
- 【LeetCode】28.实现strstr() (KMP超详细讲解,sunday解法等五种方法,java实现)
- LeetCode-35-搜索插入位置-C语言
- leetcode python28.实现strStr()35. 搜索插入位置
- Leetcode Permutation I & II
- python|leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
- Python - Search Insert Position
- LeetCode 28 Implement strStr() (C,C++,Java,Python)