C++刷题 – map和set
1.前K个高频单词
https://leetcode.cn/problems/top-k-frequent-words/
题目要求返回出现频率最高的前k个单词,如遇到相同频率的单词,应按字典序排;
这是典型的topK问题,可以考虑使用堆来解决;
方法一:使用堆和仿函数来解决
先将所有的单词都插入map中进行计数,然后再将map中的所有pair都放进优先级队列中,根据次数建大堆,出现次数相等的,根据单词进行字典序排序,这需要在仿函数中进行控制;
class Solution {
public:
struct less{
//仿函数
bool operator()(const pair<string, int>& p1, const pair<string, int>& p2)
{
if(p1.second < p2.second)//比较次数
{
return true;
}
if(p1.second == p2.second && p1.first > p2.first)//若此数相等,就按字典序排
{
return true;
}
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> countMap;
priority_queue<pair<string, int>, vector<pair<string, int>>, less> pq;//建大堆
for(const auto& str : words)//用map统计次数
{
countMap[str]++;
}
for(const auto& p : countMap)//放入堆中进行排序
{
pq.push(p);
}
vector<string> ret;
for(int i = 0; i < k; i++)//取前k个堆顶数据
{
ret.push_back(pq.top().first);
pq.pop();
}
return ret;
}
};
方法二:使用sort和仿函数来解决
先使用map来统计次数,再通过sort来排序,由于map的key是单词,所以map中的数据是按照单词的字典序排好的,从map遍历出来的数据也是按照字典序排好的,由于sort是不稳定的排序算法,所以在遇到出现次数相同的单词时,需要我们通过仿函数来控制比较;
也可以使用stable_sort来进行排序,这是一种稳定的排序算法,这样就不用再仿函数中对次数相等的单词进行控制比较了;
class Solution {
public:
struct Greater{
//仿函数
bool operator()(const pair<string, int>& p1, const pair<string, int>& p2)
{
if(p1.second > p2.second)//比较次数
{
return true;
}
if(p1.second == p2.second && p1.first < p2.first)//若次数相等,就按字典序排
{
return true;
}
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> countMap;
for(const auto& str : words)//用map统计次数 -- 默认按string去排序
{
countMap[str]++;
}
vector<pair<string, int>> sortV(countMap.begin(), countMap.end());
sort(sortV.begin(), sortV.end(), Greater());//通过sort来排降序
vector<string> ret;
for(int i = 0; i < k; i++)//取前k个数据
{
ret.push_back(sortV[i].first);
}
return ret;
}
};
方法三:使用multimap来解决
使用map排完序后,将pair的单词和次数颠倒一下放进multi_map中,针对次数进行排序;
multi_map也需要使用仿函数控制一下排降序,multi_map的排序可以认为是稳定的排序,因为对于相等的次数,后面进来的的默认会插入到前面进来的的后面,由于从map中取数据时,数据就是按照字典序排好的,这样相同次数的单词就可以默认是字典序了;
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
map<string, int> countMap;
for(const auto& str : words)//用map统计次数 -- 默认按string去排序
{
countMap[str]++;
}
multimap<int, string, greater<int>> sortMap;//multimap根据单词出现次数来排降序
for(const auto& p : countMap)
{
sortMap.insert(make_pair(p.second, p.first));
}
vector<string> ret;
auto it = sortMap.begin();
for(int i = 0; i < k; i++)//取前k个数据
{
ret.push_back(it->second);
++it;
}
return ret;
}
};
注意:不能将sortMap排成升序,然后使用反向迭代器取数据,因为因为这样不满足字典序:
multi_map排升序后,一定是i在前,love在后的,反向迭代器取数据,i和love就会先取love;
2.两个数组的交集
https://leetcode.cn/problems/intersection-of-two-arrays/
方法一
将两个vector的数据全部放进set中,完成去重,然后遍历一个set中的数据,在另一个set中find;
方法二
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
set<int> s1(nums1.begin(), nums1.end());
set<int> s2(nums2.begin(), nums2.end());
auto it1 = s1.begin();
auto it2 = s2.begin();
vector<int> ret;
while(it1 != s1.end() && it2 != s2.end())
{
if(*it1 < *it2)//set中已经是排序去重的数据了,小的迭代器++
{
it1++;
}
else if(*it2 < *it1)
{
it2++;
}
else//相等的就是交集
{
ret.push_back(*it1);
it1++;
it2++;
}
}
return ret;
}
};