【LeetCode】【前K个高频单词】

力扣

给定一个单词列表 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

 

目录

 1.使用优先级队列+仿函数

 2.map+仿函数

3.用stable_sort来保持稳定性,不在仿函数中写相同的频率的字典序排序的方法

4.使用multimap帮助排序


 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;

    }
};

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桜キャンドル淵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值