题目地址:https://leetcode.com/problems/top-k-frequent-elements/

从一个数组中求解出现次数最多的k个元素,本质是top k问题,用堆排序解决。

关于堆排序,其时间复杂度在最好和最坏的场景下都是O(nlogn)。

一开始想定义一个结构体,包含元素和元素个数两个成员,后直接用pair存储即可。

解题思路:

1. 分别统计每个数字的个数,建立数字和个数的映射,用pair存储,first=数字个数,second=数字,然后存入集合。

2. 以不同数字的个数建立大顶堆。

3.调整K次堆,依次得到K个出现次数最多的数字。

代码:

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int> topK;
if (nums.size() == ) {
return topK;
}
vector<pair<int, int>> numVec;
map<int, int> numMap;
for(int num : nums) {
if (numMap.count(num)) {
numMap[num]++;
} else {
numMap[num] = ;
}
} map<int, int>::iterator iter;
iter = numMap.begin();
while(iter != numMap.end()) {
numVec.push_back(pair<int, int>(iter->second, iter->first));
iter++;
}
int length = (int) numVec.size();
for (int i = length / - ; i >= ; i --) {
sift(i, length - , numVec);
} for (int i = length - ; i > length - k - ; i --) {
topK.push_back(numVec[].second);
swap(numVec[], numVec[i]);
sift(, i - , numVec);
} return topK;
} void sift(int low, int high, vector<pair<int, int>> &numVec) {
int i = low, j = * i + ;
while (j <= high) {
if (j < high && numVec[j].first < numVec[j+].first) {
j++;
} if (numVec[i].first < numVec[j].first) {
swap(numVec[i], numVec[j]);
i = j;
j = * i + ;
} else {
break;
}
}
} };

【leetcode】347. Top K Frequent Elements的更多相关文章

  1. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

  2. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

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

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

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

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

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

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

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

  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】

    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. CSP-2019酱油记

    炸了 Day0 上午机房弥漫着颓废的气息... nc哥在疯狂打板子,我疯狂找水题找信心(然而被历次联赛T1爆切 退役气息稍重,好多人刷屏cnblogs.. 然后年级扛把子们来做指♂导啦- 准备了火龙果 ...

  2. A#G/C013

    A#G/C013 A Sorted Arrays 不会/kk B Hamiltonish Path 我是傻逼 如果一条路径不合法,那么把不合法的端点向没出现过的相邻点连过去救星了 C Ants on ...

  3. socket数据传输

    目录 subprocess模块 struct模块: 粘包问题: QQ聊天的实现: 文件的传输: 大文件的传输: 传输层协议: TCP : UDP: FTP: socketServer模块: subpr ...

  4. python 操作es

    Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上. Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索 ...

  5. svn无法还原 、svn无法更新

    报错:  Previous operation has not finished; run 'cleanup' if it was interrupted 上一个操作尚未完成:如果中断,请运行“清理”

  6. zabbix 自动发现 监控 硬盘读写 disk io

    直接 上配置: 1.配置文件 cat userparameter_harddisk.conf #discovery hard diskUserParameter=custom.vfs.discover ...

  7. count(*) count(1) count(字段) 区别

    count(*) count(1) count(字段) 区别 count(*)和count(字段) count(*)和count(字段)使用的目的是不一样的,在必须要使用count(字段)的时候还是要 ...

  8. shebang是啥

    在计算领域中,Shebang(也称为 Hashbang )是一个由井号和叹号构成的字符序列 #! ,其出现在文本文件的第一行的前两个字符. 在文件中存在 Shebang 的情况下,类 Unix 操作系 ...

  9. JS的base64编码解码

    Unicode问题解法 有个小坑是它只支持ASCII. 如果你调用btoa("中文")会报错: Uncaught DOMException: Failed to execute ' ...

  10. Vue 自定义按键修饰符

    如点击F2 触发某个事件 <input type="button" name="" id="" value="添加" ...