给定一个单词列表 words 和一个整数 k ,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率
由高到低
排序。如果不同的单词有相同出现频率,
按字典顺序 排序
。示例 1:
输入: words = [“i”, “love”, “leetcode”, “i”, “love”, “coding”], k = 2
输出: [“i”, “love”]
解析: “i” 和 “love” 为出现次数最多的两个单词,均为2次。
注意,按字母顺序 “i” 在 “love” 之前。
示例 2:输入: [“the”, “day”, “is”, “sunny”, “the”, “the”, “the”, “sunny”, “is”, “is”], k = 4
输出: [“the”, “is”, “sunny”, “day”]
解析: “the”, “is”, “sunny” 和 “day” 是出现次数最多的四个单词,
出现次数依次为 4, 3, 2 和 1 次。
注意:
1 <= words.length <= 500
1 <= words[i] <= 10
words[i] 由小写英文字母组成。
k 的取值范围是 [1, 不同 words[i] 的数量]
进阶:尝试以 O(n log k) 时间复杂度和 O(n) 空间复杂度解决。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/top-k-frequent-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
目录
3.用stable_sort来保持稳定性,不在仿函数中写相同的频率的字典序排序的方法
1.使用优先级队列+仿函数
class Solution {
public:
struct Less
{
bool operator()(const pair<string,int>&kv1,const pair <string,int>&kv2) const
{
if(kv1.second<kv2.second)
{
return true;
}
//频率相同的时候,按照字典序排序,大的在前面
if(kv1.second==kv2.second &&kv1.first>kv2.first)
{
return true;
}
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
//先统计次数
map<string,int>countMap;
for(auto& str:words)
{
countMap[str]++;
}
//topk问题
//我们可能想到用堆去做,但是这里相同的出现频率的话,我们用堆很难去保证其单词按照字典序去排序
//但是控制仿函数其实是可以实现的
// priority_queue<pair<string,int>,vector<pair<string,int>>,Less<pair<string,int>>>MaxHeap;
// for(auto&kv:countMap)
// {
// MaxHeap.push(kv);
// }
typedef priority_queue<pair<string,int>,vector<pair<string,int>>,Less>MaxHeap;
//迭代器区间初始化
MaxHeap mh(countMap.begin(),countMap.end());
vector<string> v;
while(k--)
{
v.push_back(mh.top().first);
mh.pop();
}
return v;
}
};
2.map+仿函数
class Solution {
public:
struct greater
{
bool operator()(const pair<string,int>&kv1,const pair <string,int>&kv2) const
{
if(kv1.second>kv2.second)
{
return true;
}
//频率相同的时候,按照字典序排序,小的在前面
if(kv1.second==kv2.second &&kv1.first<kv2.first)
{
return true;
}
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
//先统计次数--默认按照string去排序了
map<string,int>countMap;
for(auto& str:words)
{
countMap[str]++;
}
//将map中的数据转移到vector中,使用迭代器区间构造
vector<pair<string,int>> sortV(countMap.begin(),countMap.end());
sort(sortV.begin(),sortV.end(), greater());
vector<string> v;
for(size_t i=0;i<k;++i)
{
v.push_back(sortV[i].first);
}
return v;
}
};
3.用stable_sort来保持稳定性,不在仿函数中写相同的频率的字典序排序的方法
class Solution {
public:
struct greater
{
bool operator()(const pair<string,int>&kv1,const pair <string,int>&kv2) const
{
if(kv1.second>kv2.second)
{
return true;
}
// //频率相同的时候,按照字典序排序,小的在前面
// if(kv1.second==kv2.second &&kv1.first<kv2.first)
// {
// return true;
// }
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
//先统计次数--默认按照string去排序了
map<string,int>countMap;
for(auto& str:words)
{
countMap[str]++;
}
//将map中的数据转移到vector中,使用迭代器区间构造
vector<pair<string,int>> sortV(countMap.begin(),countMap.end());
stable_sort(sortV.begin(),sortV.end(), greater());
vector<string> v;
for(size_t i=0;i<k;++i)
{
v.push_back(sortV[i].first);
}
return v;
}
};
4.使用multimap帮助排序
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
//先统计次数--默认按照string去排序了
map<string,int>countMap;
for(auto& str:words)
{
countMap[str]++;
}
//让它是降序,顺着去取,不能是反向迭代器反着取,这样不能保证稳定性
//这里我们的multimap再插入相同的频率的数据的时候,是会在后面插入的,不会影响我们程序的稳定性
multimap<int ,string,greater<int>> sortMap;
for(auto &kv:countMap)
{
sortMap.insert(make_pair(kv.second,kv.first));
}
vector<string> v;
multimap<int,string,greater<int>>::iterator it=sortMap.begin();
for(size_t i=0;i<k;++i)
{
v.push_back(it->second);
++it;
}
return v;
}
};