赞
踩
- class Solution {
- public:
- static bool compare(pair<int, int>& m, pair<int, int>& n){
- return m.second > n.second;
- }
- vector<int> topKFrequent(vector<int>& nums, int k) {
- int n = nums.size();
- unordered_map<int, int> record;
- for(auto x : nums){
- record[x]++;
- }
- priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&compare)> pq(compare);
- for(auto x : record){
- pq.emplace(x);
- if(pq.size() > k){
- pq.pop();
- }
- }
- vector<int> res;
- while(!pq.empty()){
- res.push_back(pq.top().first);
- pq.pop();
- }
- return res;
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。