用优先队列排序,优先队列是大根堆
class Solution {public: vector topKFrequent(vector & nums, int k) { vector result; int length = nums.size(); if(length <= 0) return result; unordered_mapm; priority_queue > n; for(auto a : nums) m[a]++; for(auto a : m) n.push({a.second,a.first}); for(int i = 0;i < k;i++){ result.push_back(n.top().second); n.pop(); } return result; }};
https://www.cnblogs.com/grandyang/p/5454125.html