LeetCode 347. Top K Frequent Ele
作者:
Terence_F | 来源:发表于
2016-07-11 15:32 被阅读20次
Top K Frequent Element
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> mp;
for (auto n: nums) mp[n]++;
vector<int> res;
priority_queue<pair<int, int>> pq;
for (auto m: mp) {
pq.push(make_pair(m.second, m.first));
if (pq.size() > mp.size() - k) {
res.push_back(pq.top().second);
pq.pop();
}
}
return res;
}
};
本文标题:LeetCode 347. Top K Frequent Ele
本文链接:https://www.haomeiwen.com/subject/gcljjttx.html
网友评论