给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
例如,
给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]。
注意:
    你可以假设给定的 k 总是合理的,1 ≤ k ≤ 数组中不相同的元素的个数。
    你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。
详见:https://leetcode.com/problems/top-k-frequent-elements/description/
C++:

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> m;
priority_queue<pair<int,int>> p;
vector<int> res;
for(int num:nums)
{
++m[num];
}
for(auto a:m)
{
p.push({a.second,a.first});
}
for(int i=0;i<k;++i)
{
res.push_back(p.top().second);
p.pop();
}
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/5454125.html

347 Top K Frequent Elements 前K个高频元素的更多相关文章

  1. Top K Frequent Elements 前K个高频元素

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

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

  4. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  5. 347. Top K Frequent Elements (sort map)

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

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

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

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

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

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

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

随机推荐

  1. HTML-js 压缩上传的图片方法(默认上传的是file文件)

    //压缩图片方法 function compressImg(file,callback){ var src; var fileSize = parseFloat(parseInt(file['size ...

  2. intel compiler的表现

    好久没弄这个东西,今天突然想试下,代码没写完,以后补. #include <stdio.h> #include <stdlib.h> #include <time.h&g ...

  3. nginx 4 win10

    去下载文件 http://nginx.org/en/download.html 然后释放文件到一目录 最后执行nginx.exe.到浏览器查看localhost,界面: 在最后,别忘了,修改其80端口 ...

  4. 如何通过js在子页面调用父页面元素的click事件

    //获取父页面的某个元素var node = window.parent.document.getElementById("btnReturn");//调用该元素的Click事件 ...

  5. 【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)

    http://blog.csdn.net/stationxp/article/details/26077439 计划: 装一个虚机,ubuntu吧,14.04 Trusty Tahr. 安装Mongo ...

  6. Modify MySQL dump file the fatest way

    使用mysql命令导入mysqldump生成的sql文件时,为了提高导入速度,往往需要修改dump文件,但是面对一个几十GB的文件,这事儿就太崩溃了,最快速的方法是这么做: ( echo " ...

  7. codevs——1462 素数和

    1462 素数和  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 青铜 Bronze 题解       题目描述 Description 给定2个整数a,b 求出它们之间(不含a ...

  8. HTTP自学心得

    HTTP是一个 客户端和 服务器端请求和应答的标准(TCP).HTTP是客户端浏览器或其他程序与 Web服务器之间的应用层通信协议. HTTP一般指超文本传输协议,它是互联网应用最广泛的协议,是用于从 ...

  9. Oracle Multitenant Environment (三) Plan for a cdb

    Below tables contains contant you need to consider while planning for a CDB. Action Considerations f ...

  10. Ambari-Blueprint介绍

    Ambari-Blueprint总体介绍 ambari-blueprint主要作用是通过提供一个restAPI.调用几次API就能够创建一个集群.ambari-server解析stack下的role_ ...