***Leetcode 692. Top K Frequent Words

Posted Z-Pilgrim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了***Leetcode 692. Top K Frequent Words相关的知识,希望对你有一定的参考价值。

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

用于练习优先队列了。。实际排序也行


struct cmp 
  bool operator() (const auto& a, const auto&b) 
      if (a.second != b.second) return a.second < b.second;
      return a.first > b.first;
    
;

class Solution 
public:
    vector<string> topKFrequent(vector<string>& words, int k) 
        priority_queue< pair<string, int>, vector< pair<string, int> >, cmp > q;
        unordered_map< string, int > mp;
        for (int i = 0; i < words.size(); i++) 
            mp[ words[i] ] ++;
        
        for (auto p: mp) 
            q.push( make_pair(p.first, p.second) );
        
        vector<string> ret;
        while (!q.empty()) 
            ret.push_back( q.top().first );
            q.pop();
            if (ret.size() >= k) break;
        
        return ret;
    
;

 

以上是关于***Leetcode 692. Top K Frequent Words的主要内容,如果未能解决你的问题,请参考以下文章

***Leetcode 692. Top K Frequent Words

leetcode 692. Top K Frequent Words 题解

leetcode692 Top K Frequent Words

692. Top K Frequent Words

692. Top K Frequent Words

力扣练习006---前K个高频单词(692)