Leetcode--692-前K个高频单词

本文介绍如何使用Python解决给定单词列表中出现频率最高的前k个单词问题,通过map和vector结合,以及自定义比较函数实现排序。两种方法分别演示了利用map的迭代器和多映射的逆序插入技巧来获取并排序结果。
摘要由CSDN通过智能技术生成

题目

给一非空的单词列表,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。

示例 1:

输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
输出: ["i", "love"]
解析: "i""love" 为出现次数最多的两个单词,均为2次。
    注意,按字母顺序 "i""love" 之前。
思路:
  1. map联合vector。
  2. map中的key是唯一的,所以相同的单词在遍历结束后只会出现一次。
  3. 所以map可以对单词列表进行计数。
  4. 定义一个map的迭代器对数组进行遍历。键值对为<string,int>
  5. 将map中的元素放进数组。
  6. 对数组中的元素进行排序,将排序后的前k个单词放进另一个数组。
    注意:map本身具有排序功能,但是是根据key值。所以这里根据题目要求设置排序条件。


class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) 
    {
        typedef map<string,int> :: iterator CountMapIt;//迭代器
        map<string,int> countMap;//定义一个map,其key为字符串,value为个数的键值对
        for(auto e : words)
        {
            countMap[e]++;
        }
        //排序
        vector<CountMapIt> v;
        vector<string> m;
        CountMapIt it = countMap.begin();
        while(it != countMap.end())
        {
            v.push_back(it);
            ++it;
        }
        struct CountMapCompare
        {
            bool operator()(const CountMapIt& it1, const CountMapIt& it2)
            {
                if(it1->second == it2->second)
                    return it1->first < it2->first;
                else
                    return it1->second > it2->second;
            }
        };
        sort(v.begin(), v.end(),CountMapCompare());
        for(auto& e : v)
        {
            m.push_back(e->first);
            k--;
            if(k == 0)
            {
                break;
            }
        }
        return m;
    }
};

方法2

//map中存储的键值对<string,int>
//map中的key值默认是从小到大的
//在用一个map将第一个map中的键值对颠倒,并使用逆序插入
//前k个字符串入数组
class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) 
    {
        typedef map<string,int>:: iterator CountMapIt;

        map<string,int> countMap;
        for(auto& e : words)
        {
            countMap[e]++;
        }
        multimap<int,string,greater<int>> sortMap;//仿函数
        for(auto& e : countMap)
        {
            sortMap.insert(make_pair(e.second,e.first));
        }

        vector<string> v;
        auto rit = sortMap.begin();
        while(k--)
        {
            v.push_back(rit->second);
            rit++;
        }
        return v;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值