题目地址: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. Mac Sublime Text使用python3运行脚本(command+b)

    默认安装好sublime, 使用快捷键command+b的时候, 会使用python2版本运行 下面就改为用python3运行, 也可以python2运行 一. 新建文件 Sublime Text - ...

  2. A1139 | 玩成模拟题的DFS

    考试的时候有思路了,但是没写完.这题起码要40min写,思路太诡异了. 刚刚写了一段,只过了一个case,得了18分,还行.明日再战. #include <stdio.h> #includ ...

  3. hive基础知识一

    1. Hive是什么 1.1 hive的概念 Hive:由Facebook开源,用于解决海量(结构化日志)的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表 ...

  4. 第08组 Alpha冲刺(5/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11898112.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...

  5. 面试官问线程安全的List,看完再也不怕了!

    最近在Java技术栈知识星球里面有球友问到了线程安全的 List: 扫码查看答案或加入知识星球 栈长在之前的文章<出场率比较高的一道多线程安全面试题>里面讲过 ArrayList 的不安全 ...

  6. ImportError: DLL load failed while importing win32api: 找不到指定的模块。

    这个是用pip install pywin32安装报的一个错误 据说直接使用pip install pypiwin32安装就不会有报错 但是遇到错误还是要尝试解决一下的 pip install pyw ...

  7. 如何完美更换WordPress网站的域名

    前几天,一位WordPress王牌主机的用户问我,他的WordPress网站已经建立一年多了,现在想要修改网站使用的域名,该如何操作?这是WordPress用户经常遇到的问题.今天我们来给大家介绍一下 ...

  8. Python 使用scapy 时报:ImportError: cannot import name 'NPCAP_PATH' 解决

    解决办法:下载源码 https://github.com/secdev/scapy 下载完成后解压后里面有个scapy文件夹,把这个文件夹替换\Lib\site-packages\ 下的scapy 整 ...

  9. 树莓派3B+红外配置

    sudo apt-get install lirc /etc/lirc/lirc_options.conf #driver = devinput driver = default #device = ...

  10. 《Go语言实战》读书笔记

    <Go语言实战>中文版pdf 百度网盘: https://pan.baidu.com/s/1kr-gMzaPAn8BFZG0P24Oiw 提取码: r6rt 书籍源码:https://gi ...