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

 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. (四)mybatis之mybatis初了解

    前言:终于到mybatis啦!   Mybatis 前文有提到,Hibernate采用的是全表映射的方式,而这方式恰恰使得性能变得较差(https://www.cnblogs.com/NYfor201 ...

  2. 在SAP CRM WebClient UI中用javascript触发ABAP event

    环境:SAP CRM WebClient UI 需求:在WebClient UI里不通过用户手动点击,而是使用JavaScript代码自动触发ABAP后台的代码. 解决方案: 1. 定义一个hidde ...

  3. 自学Spring Boot

    简介: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...

  4. 创建一个文件夹用于写入UTF-8编码的文件

    实现效果: 知识运用: File类的CreateText方法 StreamWriter类的WriteLine方法 实现代码: private void button2_Click(object sen ...

  5. remote: Incorrect username or password ( access token ) fatal: Authentication failed for

    gitee推送到远程仓库时提示错误remote: Incorrect username or password ( access token )fatal: Authentication failed ...

  6. UISearchBar的应用

    当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...

  7. Leetcode 9 回文数Palindrome Number

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  8. CF-1111 (2019/2/7 补)

    CF-1111 题目链接 A. Superhero Transformation tags : strings #include <bits/stdc++.h> using namespa ...

  9. Xshell 配色方案 Ubuntu Solarized_Dark isayme

    前言 最近在用Ubuntu,发现它的配色方案挺好看的,所以查了下有没有大神做过Xshell的Ubuntu配色方案. 一看,果然还是有大佬做了这个的. 三套配色配置如下: 1. Ubuntu的Solar ...

  10. perl学习之:subs函数

    在Perl中,sub关键字主要是为了定义一个子例程,那么subs又是什么呢? 首先subs是一个函数,用于预先声明子例程,函数的参数是预声明的函数名列表.那么这个函数存在的意义是什么?首先,通过该函数 ...