[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,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]相关推荐
- [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, ...
- Leetcode - 347. Top K Frequent Elements(堆排序)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [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, ...
- leetcode 347. Top K Frequent Elements | 347. 前 K 个高频元素(大根堆)
题目 https://leetcode.com/problems/top-k-frequent-elements/ 题解 参考:leetcode 215. Kth Largest Element in ...
- 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 ...
- 347. Top K Frequent Elements 前 K 个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...
- 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 ...
- leetcode347 - Top K Frequent Elements - medium
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 力扣347:前k个高频元素---leetcode347:Top K Frequent Elements
leetcode347题目链接:https://leetcode.cn/problems/top-k-frequent-elements 目录 一.题目描述 二.思路 1.什么是优先级队列呢? 2.什 ...
最新文章
- linux wifi-tools,Linux下WiFi工具wireless_tools交叉编译,及其支持生成iwconfig使用的内核配置...
- HDU 4609 3-idiots FFT
- C#中[WebMethod]的用法,aspx、ashx、asmx
- 力扣- -正则表达式匹配
- 【Android基础】Fragment 详解之Fragment介绍
- c#中textbox属性_C#.Net中的TextBox.Visible属性与示例
- python多线程实现for循环_Python多线程实现同时执行两个while循环
- Linux 权限管理之基本权限
- Oracle数据库 【SYNONYM 同义词 シノニム】
- 换服务器原网站数据会丢失吗,ecs服务器更换操作系统后原服务器数据还在吗?...
- 表结构设计器EZDML快速上手(2019年11月版)
- 【NLP】常见的自然语言处理任务和技术
- 系统部署在服务器,如何把系统部署在云服务器
- 小白算法练习 lanqiao2017 国赛 发现环 tarjan 无向图
- 中国信通院的星火链主链支持与以太链(测试网)交互
- 安利几个翻译照片的好用软件
- 等价类划分法与边界值分析法
- word2vec基础(非常容易理解)
- python音频可视化
- 电机分类及运行状态-同步、异步、直流
热门文章
- ARTS打卡计划第6周-REVIEW-超越编码的避免项目失败的软技能
- python五行代码解决滑块验证的缺口距离识别,破解滑块验证...
- 在linux中添加字体
- [填坑]SerialPort的“端口被关闭”
- JavaScript 获取当日在今年第几周
- ESLint使用文档
- Spring Boot常见应用属性默认值
- 【Kubernetes】浅析基本概念和原理
- 什么是Redis?简述它的优缺点?
- 依次从数组a中取出一个四位数,如果该四位数连续大于该四位数以后的5个数,且该数是奇数,则把这个四位数按从小到大的顺序存入数组b中,并计算满足上述条件的四位数的个数cnt。