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

For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].

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.

思路:

首先用map统计数字出现的次数,然后再将出现的次数作为关键词,使用桶排序,然后从后遍历,返回关键字即可。

vector<int> topKFrequent(vector<int>& nums, int k){if (nums.empty())return{};map<int, int>mp;vector<vector<int>>bucket(nums.size() + 1);for (auto a:nums)mp[a]++;for (auto it : mp)bucket[it.second].push_back(it.first);vector<int>ret;for (int i = nums.size(); i >= 0 && k>0;i--){if (!bucket[i].empty()){for (int j = 0; j < bucket[i].size() && k>0; j++){ret.push_back(bucket[i][j]);k--;}}}return ret;}

后来又看到有人用优先队列存储,感觉更方便了。

 vector<int> topKFrequent(vector<int>& nums, int k) {unordered_map<int,int> map;for(int num : nums){map[num]++;}vector<int> res;// pair<first, second>: first is frequency,  second is numberpriority_queue<pair<int,int>> pq; for(auto it = map.begin(); it != map.end(); it++){pq.push(make_pair(it->second, it->first));if(pq.size() > (int)map.size() - k){res.push_back(pq.top().second);pq.pop();}}return res;}

参考:

https://discuss.leetcode.com/topic/44226/c-o-n-log-n-k-unordered_map-and-priority_queue-maxheap-solution

转载于:https://www.cnblogs.com/hellowooorld/p/7157880.html

[leetcode-347-Top K Frequent Elements]相关推荐

  1. [leetcode]347. Top K Frequent Elements

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

  2. Leetcode - 347. Top K Frequent Elements(堆排序)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  3. [swift] LeetCode 347. Top K Frequent Elements

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

  4. leetcode 347. Top K Frequent Elements | 347. 前 K 个高频元素(大根堆)

    题目 https://leetcode.com/problems/top-k-frequent-elements/ 题解 参考:leetcode 215. Kth Largest Element in ...

  5. Leetcode 347. Top K Frequent Elements--python1行解法,Java 11ms解法

    题目地址: Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nu ...

  6. 347. Top K Frequent Elements 前 K 个高频元素

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...

  7. 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...

  8. Leetcode: Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements.For example, Given [1,1,1,2 ...

  9. leetcode347 - Top K Frequent Elements - medium

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  10. 力扣347:前k个高频元素---leetcode347:Top K Frequent Elements

    leetcode347题目链接:https://leetcode.cn/problems/top-k-frequent-elements 目录 一.题目描述 二.思路 1.什么是优先级队列呢? 2.什 ...

最新文章

  1. linux wifi-tools,Linux下WiFi工具wireless_tools交叉编译,及其支持生成iwconfig使用的内核配置...
  2. HDU 4609 3-idiots FFT
  3. C#中[WebMethod]的用法,aspx、ashx、asmx
  4. 力扣- -正则表达式匹配
  5. 【Android基础】Fragment 详解之Fragment介绍
  6. c#中textbox属性_C#.Net中的TextBox.Visible属性与示例
  7. python多线程实现for循环_Python多线程实现同时执行两个while循环
  8. Linux 权限管理之基本权限
  9. Oracle数据库 【SYNONYM 同义词 シノニム】
  10. 换服务器原网站数据会丢失吗,ecs服务器更换操作系统后原服务器数据还在吗?...
  11. 表结构设计器EZDML快速上手(2019年11月版)
  12. 【NLP】常见的自然语言处理任务和技术
  13. 系统部署在服务器,如何把系统部署在云服务器
  14. 小白算法练习 lanqiao2017 国赛 发现环 tarjan 无向图
  15. 中国信通院的星火链主链支持与以太链(测试网)交互
  16. 安利几个翻译照片的好用软件
  17. 等价类划分法与边界值分析法
  18. word2vec基础(非常容易理解)
  19. python音频可视化
  20. 电机分类及运行状态-同步、异步、直流

热门文章

  1. ARTS打卡计划第6周-REVIEW-超越编码的避免项目失败的软技能
  2. python五行代码解决滑块验证的缺口距离识别,破解滑块验证...
  3. 在linux中添加字体
  4. [填坑]SerialPort的“端口被关闭”
  5. JavaScript 获取当日在今年第几周
  6. ESLint使用文档
  7. Spring Boot常见应用属性默认值
  8. 【Kubernetes】浅析基本概念和原理
  9. 什么是Redis?简述它的优缺点?
  10. 依次从数组a中取出一个四位数,如果该四位数连续大于该四位数以后的5个数,且该数是奇数,则把这个四位数按从小到大的顺序存入数组b中,并计算满足上述条件的四位数的个数cnt。