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].
其实最简单的就是想到就是用一个小顶堆实现,如果堆中元素个数小于K,则插入元素,如果大于K,则和堆顶比较,如果大于堆顶,则弹出堆顶,插入新元素。
自己实现红黑树有点难,好在C++ STL容器中,map,set和priority_queue都是实现了红黑树结构的,所以可以拿来用。首先这边上一种我最开始自己实现的算法,通过排序来实现的。
1.设置一个unorderd_map<int,int>对每一个vector中的数据进行统计,统计出个数,然后将pair<int,int>放入vector中自己定义排序的比较算法,选取前K个,最后出来时间上还可以 Beate 70%+
代码也很容易理解,如下:
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> myMap;
for( int i = 0; i < nums.size(); i++ ){
myMap[nums[i]]++;
}
vector<PAIR> mapMember;
for( auto m : myMap){
mapMember.push_back(m);
}
sort( mapMember.begin(),mapMember.end(),myCMP);
vector<int> result;
for( int i = 0; i < k; i++ ){
result.push_back(mapMember[i].first);
}
return result;
}
private:
typedef pair<int,int> PAIR;
static bool myCMP (PAIR& num1, PAIR& num2){
return num1.second > num2.second;
}
};
思路2:用最小堆:首先需要了解一下C++中的优先队列priority_queue进行一个了解,priority_queue 优先级队列是一个拥有权值概念的单向队列queue,在这个队列中,所有元素是按优先级排列的(也可以认为queue是个按进入队列的先后做为优先级的优先级队列——先进入队列的元素优先权要高于后进入队列的元素)。他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。第一个是元素类型,这边的话就是pair<int,int> 第二个是保存数据的容器,一般默认使用vector,第三个是比较方式,如果不定义,默认是大顶堆,这边因为需要小顶堆实现,所以使用greater<>;
代码:
typedef pair<int, int> P;
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
for (int x : nums) cnt[x] ++;
priority_queue<P, vector<P>, greater<P> > q;
for (auto &x : cnt) {
if (q.size() < k)
q.push(make_pair(x.second, x.first));
else {
if (q.top().first < x.second) {
q.pop();
q.push(make_pair(x.second, x.first));
}
}
}
vector<int> ans;
while (!q.empty()) {
ans.push_back(q.top().second);
q.pop();
}
return ans;
}
};
这边需要注意,这里pair中,first和second通过make_pair掉位置了,为什么呢?因为对于pair来说,默认的比较操作是比较first成员的,但是我们这边需要比较的second的数据,所以需要调一个位置。
LeetCode 【347. Top K Frequent Elements】的更多相关文章
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 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 ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
- 【leetcode】347. Top K Frequent Elements
题目地址:https://leetcode.com/problems/top-k-frequent-elements/ 从一个数组中求解出现次数最多的k个元素,本质是top k问题,用堆排序解决. 关 ...
- 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 解题思路 - Java
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
随机推荐
- json字符串返回到js中乱码
Ajax 的post请求值返回到js中时出现中文乱码的情况,但是在action中写入时并未乱码,解决办法在action中写入前,加上这两行: request.setCharacterEncoding( ...
- VMware-workstation-full-11.0.0-2305329&VMware-player-7.0.0-2305329
VMware-workstation-full-11.0.0-2305329.exe Name: VMware-workstation-full-11.0.0-2305329.exe 发行日期: 20 ...
- CDS
very nice artical talk about mergechangelog and cleardataset Delta and Data http://www.cnblogs.com/y ...
- 一款公用的CSS+DIV弹窗
为了方便以后自己使用! <html> <head> <style> .winmainshow { background: #fff; padding: 10px 5 ...
- fzu2028
//Accepted 7324 KB 203 ms /* source:fzu2028 time :2015.5.29 by :songt */ /*题解: 树链剖分 单点更新,求路径和 */ #in ...
- Json 讲解
JSON详解 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如何使用JSON 在.NET中如何使用JSON 总结 JSON的全称是”JavaScript Object Notation”, ...
- deProto原型设计工具
现有的原型工具,比如axure rp等等,都或多或少存在一些使用比较复杂的问题.以至于常常属于少数的产品经理的技能型工具. 在业余的时候,自己凭借兴趣开发了这个工具.希望能够以更简便的方法制作一个de ...
- nexus7 一代 手动刷4.4.4
跟上一篇类似,但是中间出了点问题,提示说command write 出错,最后解决方法是电脑上换了个usb口插入....
- BZOJ 1799 同类分布
一开始没想出来..一看题解 我艹直接枚举数位的和啊.....怪不得给50s. 还是太蠢. #include<iostream> #include<cstdio> #includ ...
- C#双链表
单链表允许从一个结点直接访问它的后继结点,所以, 找直接后继结点的时间复杂度是 O(1).但是,要找某个结点的直接前驱结点,只能从表的头引用开始遍历各结点.如果某个结点的 Next 等于该结点,那么, ...