码迷,mamicode.com
首页 > 其他好文 > 详细

692. Top K Frequent Words

时间:2018-05-03 14:26:16      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:span   ems   opera   first   amp   http   ==   div   priority   

https://leetcode.com/problems/top-k-frequent-words/description/

class ComparisonClass {
public:
    bool operator() (pair<int,string> p1, pair<int,string> p2) {
        return p1.first < p2.first || p1.first == p2.first && p1.second > p2.second;
    }
};
class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string,int> freq;
        for (const auto& w : words)
            freq[w]++;
        priority_queue<pair<int,string>, vector<pair<int,string>>, ComparisonClass> q;
        for (const auto& f : freq)
            q.push( { f.second, f.first });
        
        vector<string> res;
        while (k-- > 0 && !q.empty()) {
            res.push_back(q.top().second);
            q.pop();
        }
        return res;
    }
};

 

692. Top K Frequent Words

标签:span   ems   opera   first   amp   http   ==   div   priority   

原文地址:https://www.cnblogs.com/JTechRoad/p/8984842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!