【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/top-k-frequent-elements/description/
题目描述
Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].
Note:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
解题方法
把出现次数最大的前k个数输出。
解题方法
字典
这个题要求时间复杂度是O(nlogn),就可以按照出现的次数先排个序,然后找到出现最多的k个就好。Counter类有most_common()函数,能按出现的次数进行排序。返回的是个列表,列表中每个元素都是一个元组,元组的第一个元素是数字,第二个数字是出现的次数。
from collections import Counter
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
counter = Counter(nums).most_common()
return [counter[i][0] for i in range(k)]
优先级队列
使用优先级队列来让出现次数多的优先弹出来,当然需要字典统计次数。
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int, int> m;
for (int n : nums) m[n] ++;
priority_queue<pair<int, int>> p;
for (auto a : m)
p.push({a.second, a.first});
vector<int> res;
for (int i = 0; i < k; i++) {
res.push_back(p.top().second); p.pop();
}
return res;
}
};
日期
2018 年 2 月 8 日
2018 年 12 月 14 日 —— 12月过半,2019就要开始
【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)的更多相关文章
- [LeetCode] 347. Top K Frequent Elements 解题思路 - Java
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 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 ...
- Java [Leetcode 347]Top K Frequent Elements
题目描述: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...
- [leetcode]347. Top K Frequent Elements 最高频的前K个元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 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 ...
随机推荐
- eggNOG 5.0数据库介绍
目录 1. eggNOG简介 2. eggNOG-Mapper注释原理 3. eggNOG 5.0数据资源 4. eggNOG-Mapper使用 5. NOG.KOG.COG.KEGG.GO区别? 1 ...
- UE4打包启动失败:RunUAT.bat ERROR: AutomationTool failed to compile.
打包配置正常的情况下,出现下面Log: RunUAT.bat ERROR: AutomationTool failed to compile. 基本上,可以先排查下任务管理器中是不是有UE4Edito ...
- UE4类型数据自动注册
Version:4.26.2 UE4 C++工程名:MyProject 在<宏GENERATED_BODY做了什么?>中,简单分析了GENERATED_BODY宏给一个简单的.继承自UOb ...
- myatoi
atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中.int atoi(const char *nptr) 函数会扫描参数 nptr字符串 ...
- Android 清除本地缓存
主要功能:清除内.外缓存,清除数据库,清除Sharepreference,清除files和清除自定义目录 public class DataCleanManager { //清除本应用内部缓存(/da ...
- Linux基础命令---mirror获取ftp目录
mirror 使用lftp登录ftp服务器之后,可以使用mirror指令从服务器获取目录 1.语法 mirror [OPTS] [source [target]] 2.选项列表 选 ...
- 【Linux】【Services】【SaaS】Docker+kubernetes(11. 构建复杂的高可用网络)
1. 简介 flannel在实战阶段貌似不能胜任在灾难恢复时候异地的网络,打算用openvswith试试
- linux-源码软件管理-yum配置
总结如下:1.源码配置软件管理2.配置yum本地源和网络源及yum 工作原理讲解3.计算机硬盘介绍 1.1 源码管理软件 压缩包管理命令: # 主流的压缩格式包括tar.rar.zip.war.gzi ...
- MySQL索引及性能优化分析
一.SQL性能下降的原因 查询语句问题,各种连接.子查询 索引失效(单值索引.复合索引) 服务器调优及各个参数设置(缓冲.线程池等) 二.索引 排好序的快速查找数据结构 1. 索引分类 单值索引 一个 ...
- jquery:iframe里面的元素怎样触发父窗口元素的事件?
例如父窗口定义了一个事件. top: $(dom1).bind('topEvent', function(){}); 那么iframe里面的元素怎样触发父窗口dom1的事件呢?这样吗? $(dom1, ...