LeetCode 692. Top K Frequent Words
2017-12-23 13:00
330 查看
题目链接:https://leetcode.com/problems/top-k-frequent-words/description/
template<class _Ty1, class _Ty2> struct MyPairLess { typedef _Ty1 first_argument_type; typedef _Ty2 second_argument_type; typedef bool result_type; constexpr bool operator() (const pair<_Ty1, _Ty2>& lhs, const pair<_Ty1, _Ty2>& rhs) const { return lhs.first < rhs.first || (!(rhs.first < lhs.first) && lhs.second > rhs.second); } }; class Solution { public: vector<string> topKFrequent(vector<string>& words, int k) { vector<string> res; priority_queue < pair<int, std::string>, vector<pair<int, std::string>>, MyPairLess<int, std::string> > pq; unordered_map<std::string, int> hash; for (std::string s : words) hash[s]++; for (auto it = hash.begin(); it != hash.end(); ++it) pq.push(make_pair(it->second, it->first)); for (int i = 0; i < k; ++i) { res.push_back(pq.top().second); pq.pop(); } return res; } };
相关文章推荐
- leetcode 692. Top K Frequent Words
- [Leetcode] 692. Top K Frequent Words 解题报告
- LeetCode算法题——Top K Frequent Words
- [LeetCode] Top K Frequent Words 前K个高频词
- 692. Top K Frequent Words
- LeetCode Top K Frequent Words
- leetcode692 Top K Frequent Words
- leetcode151: Reverse Words in a String
- leetcode 347.Top K Frequent Elements
- LeetCode---Reverse Words in a String
- [leetcode]Reverse Words in a String(using Python)
- LeetCode 30 Substring with Concatenation of All Words (C,C++,Java,Python)
- LeetCode | Reverse Words in a String
- Leetcode: Substring with Concatenation of All Words
- 【LeetCode】Reverse Words in a String 解题报告
- leetcode - Reverse Words in a String
- LeetCode Integer to English Words(整数转化为英文字母)
- LeetCode Top K Frequent Elements
- Leetcode||30.Substring with Concatenation of All Words
- [Leetcode Solution] Reverse Words in a String