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 ...
随机推荐
- SQL获取汉字首字母
)) ) as begin ) ) collate Chinese_PRC_CI_AS,letter )) insert into @t(chr,letter) select '吖','A' unio ...
- freeCAD特性列表
通用特性 基本应用 FreeCAD 是跨平台的. 它在 Windows Linux 和 Mac OSX 等平台上运行表现一致. FreeCAD 是图形化应用程序. FreeCAD 基于著名的 GUI ...
- jsp中的正则表达式
sp中${}----是EL表达式的常规表示方式目的是为了获取{}中指定的对象(参数.对象等)的值如:${user.name}<====>User user = (User)request( ...
- Bzoj索引
1001 : http://ideone.com/4omPYJ1002 : http://ideone.com/BZr9KF1003 : http://ideone.com/48NJNh1004 : ...
- PHP面向对象实例(图形计算器)
效果:
- grep 命令操作
linux grep命令 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...
- Protobuf - 使用scons编译proto文件
使用protobuf过程中,需要先对消息结构进行定义,文件以.proto格式结尾.然后要使用google提供的protoc命令行,把.proto文件转成对应的代码文件. protoc --proto_ ...
- Linux内核分析——分析system_call中断处理过程
万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我选择的是get ...
- Python-day3作业-haproxy配置文件管理脚本
#!/usr/bin/env python import os,sys,time,re,prettytable,json from collections import defaultdict,Ord ...
- CSS前5课总结
CSS<精通CSS.DIV网页样式与布局>视频前5课总结: 地对地导弹 第一课: 使用CSS控制页面: 1,行内样式 <p style="color:#0000FF; fo ...