主要是对次数进行排序,然后去前几个最大次数的值,输出即可

 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的使用的更多相关文章

  1. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. [leetcode]347. Top K Frequent Elements K个最常见元素

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

  3. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

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

  4. [LeetCode]347. 前 K 个高频元素(堆)

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

  5. 【LeetCode】677. Map Sum Pairs 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 前缀树 日期 题目地址:https://lee ...

  6. [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 ...

  7. [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 ...

  8. Java [Leetcode 347]Top K Frequent Elements

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

  9. leetcode 347前k个高频元素

    通过hash map遍历一遍存储出现的次数,通过小顶堆存储k个元素 //设想利用hash map来存储每个元素的个数:采用小顶堆存储k个元素:timeO(n+klogk)spaceO(n+k) cla ...

随机推荐

  1. Openjudge 1.13-21:最大质因子序列

    总时间限制:  1000ms 内存限制:  65536kB 描述 任意输入两个正整数m, n (1 < m < n <= 5000),依次输出m到n之间每个数的最大质因子(包括m和n ...

  2. SAP Cloud for Customer(C4C)的一些学习资料

    经常有顾问朋友们问我想自学C4C,有什么好的资料. SAP内部确实有一些C4C培训材料,但是不能散布到公司外部. 想学习C4C,还是得到SAP官网网站上查找资料. 1. 登录https://help. ...

  3. poj1595 水题

    题意:输入n, 和c  统计1 - n 有多少个素数为cnt 若 2*c > cnt 则将素数全部输出 否则分支判断:  若cnt 为偶数,则从中心开始输出2*c 个  若cnt 为奇数,则从中 ...

  4. 爬虫_python3_urllib

    urlib库为python3的HTTP内置请求库 urilib的四个模块: urllib.request:用于获取网页的响应内容 urllib.error:异常处理模块,用于处理异常的模块 urlli ...

  5. java web.xml被文件加载过程及加载顺序小结

    web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...

  6. PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)

    PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)    http://www.patest.cn/contests/pat-b-practise/10 ...

  7. ios copy assign retain

    一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...

  8. PAT 乙级 1012

    题目 题目地址:PAT 乙级 1012 思路 最后一个测试点怎么也过不了,问题在于A2的判断,不能单纯地以0作为判断条件:假设满足A2条件的只有两个数6和6,计算结果仍然是0,但是输出A2的值是0不是 ...

  9. PHP 线上项目 无法操作

    部署到线上的项目,http 环境没有问题,首页展示没有问题,但是跳转页面展示到了本地, 解决办法 : 更改文件夹所属用户 chown -R apache:apache html

  10. 五:SQL语句中的数据类型

    一:MySQL数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的 MySQL支持多种数据类型,大致可以分为三类:数值 日期/时间和字符串 二.数值类型(12) 2.1.整数类型(6) ...