Given a non-empty array of integers, return the k most frequent elements.

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

Note:

  • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  • Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

这道题给了我们一个数组,让统计前k个高频的数字,那么对于这类的统计数字的问题,首先应该考虑用 HashMap 来做,建立数字和其出现次数的映射,然后再按照出现次数进行排序。可以用堆排序来做,使用一个最大堆来按照映射次数从大到小排列,在 C++ 中使用 priority_queue 来实现,默认是最大堆,参见代码如下:

解法一:

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> m;
priority_queue<pair<int, int>> q;
vector<int> res;
for (auto a : nums) ++m[a];
for (auto it : m) q.push({it.second, it.first});
for (int i = ; i < k; ++i) {
res.push_back(q.top().second); q.pop();
}
return res;
}
};

当然,既然可以使用最大堆,还有一种可以自动排序的数据结构 TreeMap,也是可以的,这里就不写了,因为跟上面的写法基本没啥区别,就是换了一个数据结构。这里还可以使用桶排序,在建立好数字和其出现次数的映射后,按照其出现次数将数字放到对应的位置中去,这样从桶的后面向前面遍历,最先得到的就是出现次数最多的数字,找到k个后返回即可,参见代码如下:

解法二:

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> m;
vector<vector<int>> bucket(nums.size() + );
vector<int> res;
for (auto a : nums) ++m[a];
for (auto it : m) {
bucket[it.second].push_back(it.first);
}
for (int i = nums.size(); i >= ; --i) {
for (int j = ; j < bucket[i].size(); ++j) {
res.push_back(bucket[i][j]);
if (res.size() == k) return res;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/347

类似题目:

Kth Largest Element in an Array

Word Frequency

Sort Characters By Frequency

Split Array into Consecutive Subsequences

Top K Frequent Words

K Closest Points to Origin

参考资料:

https://leetcode.com/problems/top-k-frequent-elements/

https://leetcode.com/problems/top-k-frequent-elements/discuss/81602/Java-O(n)-Solution-Bucket-Sort

https://leetcode.com/problems/top-k-frequent-elements/discuss/81635/3-Java-Solution-using-Array-MaxHeap-TreeMap

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. web前端教程之javascript创建对象的方法
  2. ecshop 网站标题不更新或内容不更新
  3. hdu 4861 Couple doubi (找规律 )
  4. CentOS 安装jdk1.7 64位
  5. ORACLE 数据库简单测试
  6. [原]命令模式在MVC框架中的应用
  7. MVC模式已死
  8. python学习笔记之三:字典,当索引不好用时
  9. 洛谷-谁拿了最多奖学金-NOIP2005提高组复赛
  10. Wiegand协议(转)
  11. Redis学习笔记~Twenproxy所起到的作用
  12. phpStudy The requested URL /web/index.php was not found on this server
  13. Method not found: !!0[] System.Array.Empty()错误
  14. iOS开发-UISwipeGestureRecognizer滑动手势
  15. pjax 和 ajax 的区别
  16. 「专题训练」Collecting Bugs(POJ-2096)
  17. 关于jQuery Form Plugin使用心得
  18. Python 必备神器
  19. Suricata开源IDS安装与配置
  20. 10 Things ASP.NET Developers Should Know About Web.config Inheritance and Overrides(转)

热门文章

  1. ubuntu16.04 下anaconda3安装教程
  2. LocalDB 从2017更换到2014后一直显示连接不正确解决方案
  3. MySQL for OPS 08:MHA 高可用
  4. java架构之路(mysql底层原理)Mysql之Explain使用详解
  5. Visual Studio 2017使用ODT 连接Oracle 数据库出现异常
  6. 咕咕咕-HLPP算法
  7. Java自学-集合框架 Collections
  8. Java自学-集合框架 ArrayList常用方法
  9. 关于创建node服务
  10. 使用Composer安装阿里云短信失败