作者: 负雪明烛
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:

  1. You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  2. 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++)的更多相关文章

  1. [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 ...

  2. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  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. 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 ...

  6. Java [Leetcode 347]Top K Frequent Elements

    题目描述: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...

  7. [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 ...

  8. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  9. 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. Ansi,UTF8,Unicode,ASCII编码的区别

    Ansi,UTF8,Unicode,ASCII编码的区别 近日需要不同的编码,关于上述编码,一直迷迷糊糊,查了些资料,总算大致了解了, 下面全是从网上搜来的: 1.  ASCII和Ansi编码     ...

  2. mysql-计算排名

    mysql计算排名,获取行号rowno 学生成绩表数据 SELECT * FROM table_score ORDER BY score DESC; 获取某个学生成绩排名并计算该学生和上一名学生成绩差 ...

  3. mac系统升级导致VirtualBox报Kernel driver not installed (rc=-1908)

    一.背景 最近将我的Mac升级成了Monterey版本,结果发现之前的安装的VirtualBox虚拟机无法启动,报了如下错误. Kernel driver not installed (rc=-190 ...

  4. jsp页面中HTML注释与jsp注释的区别

    jsp页面中HTML注释与jsp注释的区别 HTML注释 html注释是 : HTML注释:参与编译,会生成到源码中. 所以,不能使用html注释EL表达式和JSTL标签库 jsp注释 jsp注释是 ...

  5. 在C++的map类型中按value排序

    1.将map转化为vector类型 2.使用sort函数对vector进行排序,写出compare比较器函数 3.比较器中指明按照第几个元素来排序 1 #include <iostream> ...

  6. ace

    ace An ace is a playing card, die or domino with a single pip. In the standard French deck, an ace h ...

  7. 转Android Canvas和Paint基本使用

    Android Canvas和Paint基本使用   这篇文章主要介绍下画笔Paint和画布Canvas的基本使用  1.Paint 创建对象Paint mPaint = new Paint(); 常 ...

  8. JVM堆空间结构及常用的jvm内存分析命令和工具

    jdk8之前的运行时数据区域 程序计数器 是一块较小的内存空间,它可以看做是当前线程所执行的字节码的行号指示器.每个线程都有一个独立的程序计数器,这类内存区域为"线程私有",此内存 ...

  9. HashMap的putAll方法介绍说明

    jdk1.8 使用putAll时,新map中的值仅为旧map值所对应对象的引用,并不会产生新对象. 如下,使用for循环赋值! public void putAll(Map<? extends ...

  10. Mysql主从复制参数详解

    目录 一.简介 二.例子 同步 修改 三.参数 一.简介 change master to配置和改变slave服务器用于连接master服务器的参数,以便slave服务器读取master服务器的bin ...