zoukankan      html  css  js  c++  java
  • 692. Top K Frequent Words

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

    class Solution {
    public:
        vector<string> topKFrequent(vector<string>& words, int k) {
            unordered_map<string, int> freq;
            for (auto & w : words)
                freq[w]++;
            
            priority_queue<pair<int,string>> q;
            
            for (auto & f : freq)
            {
                q.push({-f.second, f.first});
                if (q.size() > k)
                    q.pop();
            }
            
            vector<string> res;
            while (!q.empty())
            {
                res.push_back(q.top().second);
                q.pop();
            }
            
            reverse(res.begin(), res.end());
            
            return res;
        }
    };
    

      

  • 相关阅读:
    深入理解多态..............................
    走过路过 不要错过..
    进军C#..
    员工打卡....
    MySQL
    MySQL
    MySQL
    MySQL
    MySQL
    MySQL
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9957666.html
Copyright © 2011-2022 走看看