leetcode 347 priority,map的使用
主要是对次数进行排序,然后去前几个最大次数的值,输出即可
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
int i;
unordered_map<int,int>p_map;
map<int,int>::iterator it;
for(i=;i<nums.size();i++) {
p_map[nums[i]]++;
}
priority_queue<pair<int,int>>p_queue;
for(it=p_map.begin();it!=p_map.end();it++)
p_queue.push(make_pair(it->second,it->first));
vector<int>num_result;
for(i=;i<=k;i++) {
num_result.push_back(p_queue.top().second);
p_queue.pop();
}
return num_result;
}
};
另一种写法也非常好:http://blog.csdn.net/yzhang6_10/article/details/51388021
class Op{
public:
int Op_count;
int Op_num;
Op(int count,int num) {
Op_count=count;
Op_num=num;
}
bool operator <(const Op&Cpone)const
{
return Op_count>Cpone.Op_count;
}
};
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
int i;
map<int,int>p_map;
map<int,int>::iterator it;
for(i=;i<nums.size();i++) {
p_map[nums[i]]++;
}
vector<Op>p_queue;
for(it=p_map.begin();it!=p_map.end();it++)
p_queue.push_back(Op(it->second,it->first));
vector<int>num_result;
sort(p_queue.begin(),p_queue.end());
for(i=;i<k;i++)
num_result.push_back(p_queue[i].Op_num);
return num_result;
}
};
leetcode 347 priority,map的使用的更多相关文章
- 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 = [ ...
- [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. 前 K 个高频元素(堆)
题目 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1 ...
- 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...
- [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 ...
- [leetcode]347. Top K Frequent Elements 最高频的前K个元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- Java [Leetcode 347]Top K Frequent Elements
题目描述: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...
- leetcode 347前k个高频元素
通过hash map遍历一遍存储出现的次数,通过小顶堆存储k个元素 //设想利用hash map来存储每个元素的个数:采用小顶堆存储k个元素:timeO(n+klogk)spaceO(n+k) cla ...
随机推荐
- shell脚本,批量创建10个系统帐号并设置密码为随机8位字符串。
[root@localhost wyb]# cat user10.sh #!/bin/bash #批量创建10个系统帐号wangyb01-wangyb10并设置密码(密码为随机8位字符串). > ...
- java在线聊天项目1.3版 ——设计好友列表框功能
设计好友列表框功能,思路—— 1.当客户端成功登陆后,则客户端把成功登陆信息发送给服务端, 2.由服务端将接收到来自各个成功登陆的客户端的用户信息添加进好友列表, 3.每当有成功登陆的用户就向各个客户 ...
- ios xmpp demo
为了方便程序调用,我们把XMPP的一些主要方法写在AppDelegate中 在AppDelegate.m下这几个方法为: [java] view plaincopy -(void)setupStrea ...
- ios设备屏幕尺寸与分辨率
iOS 设备的屏幕尺寸.分辨率及其屏幕边长比例详细情况是怎样的? 根据屏幕尺寸和分辨率,ios现在数起来有6个版本.一,3GS:二,4s为代表:三,iphone5:四,ipad2为代表:五,ipad4 ...
- 74个Swift标准库函数
74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...
- ES6变量解构赋值的用法
一.数组赋值(从数组中提取值,按照对应位置,对变量赋值) 1. 完全解构(变量与值数目相等) let arr = [1, 2, 3]; let [a,b,c] = arr; console.log(a ...
- STL 之 vector的应用
关于vector vector是C++提供的一个容器,它是一个能够存放任意类型的动态数组,可以随时增加和压缩数据. 使用vector时需要注意以下几点: 1. 如果要表示的向量长度较长(需要为向量内部 ...
- nginx正则配置解释和fastadmin
参考:http://www.cnblogs.com/netsa/p/6383094.html 1 2 3 4 5 6 7 8 9 10 11 1.^: 匹配字符串的开始位置: 2. $:匹配字符串 ...
- 时间格式的处理和数据填充和分页---laravel
时间格式文档地址:http://carbon.nesbot.com/docs/ 这是些时间格式,只需要我们这么做就可以 我们在模板层,找到对应的模型对象那里进行处理就可以啦 2018-11-08 16 ...
- Google实践中总结的Python规范,get了吗?
好的代码风格,给人舒服的感觉,今天介绍一下谷歌的Python风格规范 1 分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 2 行长度 每行不超过80个字符:不要使用反斜杠连接行.Pyth ...