题目地址: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. Using the Repository and Unit Of Work Pattern in .net core

    A typical software application will invariably need to access some kind of data store in order to ca ...

  2. K8s无状态控制器原理介绍

    Pod控制器: ReplicationController:早期K8s只有这一个控制器,但后来发现让这一个来完成所有任务,太复杂.因此被废弃. ReplicaSet: 它用于帮助用户创建指定数量的Po ...

  3. 第09组 Alpha冲刺(4/6)

    队名:观光队 组长博客 作业博客 组员实践情况 王耀鑫 过去两天完成了哪些任务 文字/口头描述 完成服务器连接数据库部分代码 展示GitHub当日代码/文档签入记录 接下来的计划 服务器网络请求,vu ...

  4. Nexus OSS私服仓库的备份与迁移

    背景 在上一篇博客 [Maven学习]Nexus OSS私服仓库的安装和配置 中,我们已经在机房搭建好了新的Nexus OSS私服仓库.下面是两个版本的Nexus OSS私服仓库的对比图. 老的Nex ...

  5. python:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

    # 将默认编码设为utf-8 # 否则会报错: # UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ...

  6. Appium-Python-Windows 环境搭建

    目录 1.安装JDK 2.安装Android SDK 3.安装Node.js 4.安装Appium server 5.安装Python 6.安装Appium-Python-Client 7.安装pyt ...

  7. java命名总结

    下文主要来源于网上,我做了一些编辑整理. “如果你不知道一件事物叫什么, 你就不知道它是什么. 如果你不知道这是什么, 你就不可能坐下来写代码.” ----萨姆·加德纳(Sam Gardiner) 1 ...

  8. ES6 - 函数扩展(函数参数默认值)

    函数参数默认值 ES6 之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log(x, y); ...

  9. Embed MP4 in HTML using flash-player(html5 video player)

    https://stackoverflow.com/questions/1000851/embed-mp4-in-html-using-flash-player ******************* ...

  10. TrueType字体

    TrueType字形描述技术和TTF文件 TrueType字体技术的研究分析与应用 看起来上面两篇都是翻译的文档. 看微软的原文介绍: TrueType fundamentals 其他文章: 获取Tr ...