题目地址: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. PSQLException: An I/O error occurred while sending to the backend.

    postgresql批量新增数据时,批量插入的数据量太大了,造成了IO异常 只能把这么多数据先进行分割,再批量插入,但是感觉这种方式不是最优解,先暂时顶着,具体List分割方式如下: package ...

  2. Gamma阶段第一次scrum meeting

    每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化:发现用户体验细节问题https://github.com/rRetr0Git/rateMyCourse/issues ...

  3. Java编程思想之四控制执行流程

    程序必须再执行过程中控制它的世界,并做出选择.在Java中,你要使用执行控制语句来做出选择. 4.1true和false 所有条件语句都利用条件表达式的真或假来决定执行路径. Java不允许使用数字作 ...

  4. Django实现自动发布(1数据模型)

    公司成立之初,业务量较小,一个程序包揽了所有的业务逻辑,此时服务器数量少,上线简单,基本开发-测试-上线都是由开发人员完成. 随着业务量逐渐上升,功能增多,代码量增大,而单一功能上线需要重新编译整个程 ...

  5. Oracle 行转列 动态出转换的列

    本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ...

  6. AES采用CBC模式128bit加密工具类

    写在前面 安全测试ECB模式过于简单需要改为CBC模式加密以下为工具类及测试 AESUtils.java package com.sgcc.mobile.utils; import sun.misc. ...

  7. Quartus Prime 与 Modelsim 调试 及do文件使用

    Quartus Prime 与 Modelsim 调试 及do文件使用 2019-06-28 11:12:50 RushBTaotao 阅读数 49更多 分类专栏: IntelFPGA-Softwar ...

  8. android studio如何连接夜神模拟器

    原创 2018-02-05 21:35:03 会飞的鱼儿android 阅读数 16706 文章标签: 夜神模拟器连接夜神模拟器的简单方式 更多 分类专栏: Android   版权声明:本文为博主原 ...

  9. 使用Flume-Taildir和rocketmq-flume与RocketMQ的结合

    一.Fume-Taidir Flume1.7.0加入了taildirSource作为agent的source.可以说是 Spooling Directory Source + Exec Source ...

  10. SpringMVC 事件监听 ApplicationListener

    1. 实现 ApplicationListener<T> 接口(T为监听类型,稍后会列出具体可监听事件) 2. 将该自定义监听类,注册为Spring容器组件.(即将该类注入Spring容器 ...