题目地址: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. send 和recv小结

    不论是客户还是服务器应用程序都用send函数来向TCP连接的另一端发送数据. 不论是客户还是服务器应用程序都用recv函数从TCP连接的另一端接收数据. #include <sys/socket ...

  2. C# 全角半角字符互转

    /// <summary> /// 全角空格为12288,半角空格为32 /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 /// < ...

  3. NodeJS基础学习总结

    一.nodeJS解释 JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器. 每一种解析器都是 ...

  4. nginx 正向代理配置

    需求场景:从以下俩张图可以比较直观的理解正向代理的作用(在其他文章中会表示为“http代理”,注意当前文档的配置不支持https代理) Nginx正向代理配置文件: server{ listen de ...

  5. nginx 配置虚拟主机( 基于端口 )

    一.创建网站目录及文件: [root@localhost data]# tree /data /data └── wwwroot ├── www.1.com_8080 │   └── index.ht ...

  6. 转载:关于思科交换机、路由器如何关闭telnet 开启ssh服务

    等保测评要求: 必须关闭telnet服务,开启ssh服务 即用ssh方式登录网络设备,而不允许用telnet. 输入密码.en 再次输入密码.sh run 这些常规动作就不再赘述. 1.关闭telne ...

  7. IT项目经理都需要具备哪些能力

    发布时间:05-2009:24优质原创作者 项目经理是IT行业中比较常见的职位,工作职责主要包括三个方面,其一是资源整合任务:其二是沟通协调任务:其三是保障项目的时间周期. 资源整合能力是项目经理的重 ...

  8. CentOS7安装及配置vsftpd (FTP服务器FTP账号创建以及权限设置)

    本文章向大家介绍CentOS7安装及配置vsftpd (FTP服务器FTP账号创建以及权限设置),主要包括CentOS7安装及配置vsftpd (FTP服务器FTP账号创建以及权限设置)使用实例.应用 ...

  9. pdf怎么转换成word

    在线转换:https://app.xunjiepdf.com/pdf2word

  10. Wordpress 安装或切换不同的版本

    如果升级到最新版本的 Wordpress 后,发现有 bug,需要回滚回上一个相对稳定的版本,可以按照如下步骤: 一.到官网下载压缩包 https://wordpress.org/download/r ...