"""
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
"""
"""
用dict实现的木桶排序
解法一:木桶+sort
解法二:木桶+heap(堆)
解法三:维护一个n-k的最大堆((有局限性,如果是第k大,会出问题))。此题是前k大的都append里面了
"""
class Solution1:
def topKFrequent(self, nums, k):
count_list = dict()
res = []
for num in nums:
count_list[num] = count_list.get(num, 0) + 1
#如果count_list[num]没有value,则value是0,否则是value+1
#dict.get(key, default=None)
#key -- 字典中要查找的键。
#default -- 如果指定键的值不存在时,返回该默认值。
t = sorted(count_list.items(), key=lambda l: l[1], reverse=True)
#sorted(iterable, key=None, reverse=False) 返回的是一个list
#iterable -- 可迭代对象
#key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可 #迭代对象中的一个元素来进行排序。
#lambda 输入l是(key,value),输出l[1]是value. 可以理解l[0]是key
#reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
for i in range(k):
res.append(t[i][0]) # t[i][0]表示第i个tuple的第1个元素,t[i][1]则表示第二个元素
return res """
解法二:木桶+heap
python heap
nums = [2, 3, 5, 1, 54, 23, 132]
heap = []
for num in nums:
heapq.heappush(heap, num) # 第一种加入堆
heapq.heapify(nums) #第二种生成堆
print([heapq.heappop(heap) for _ in range(len(nums))]) # 堆排序结果
nums = [1, 3, 4, 5, 2]
print(heapq.nlargest(3, nums)) [5, 4, 3]
print(heapq.nsmallest(3, nums)) [1, 2, 3]
"""
class Solution2:
def topKFrequent(self, nums, k):
import heapq
count_list = dict()
for num in nums:
count_list[num] = count_list.get(num, 0) + 1
p = list() #存储堆排序后的结果
for i in count_list.items():
heapq.heappush(p, (i[1], i[0])) #加入堆,每个结点是个tuple(,)
return [i[1] for i in heapq.nlargest(k, p)] #这里的i[1] 其实是上一行的i[0]
"""
堆默认root是最小值
解法三:维护一个n-k的最大堆(有局限性,最大的值最后入堆,会出问题)
最大堆需要将最小堆的值取反
"""
class Solution3:
def topKFrequent(self, nums, k):
import heapq
count_list = dict()
for num in nums:
count_list[num] = count_list.get(num, 0) + 1
p = list()
res = list()
for i in count_list.items():
heapq.heappush(p, (-i[1], -i[0])) #bug前面没写heapq
if len(p) > len(count_list) - k:
_, val = heapq.heappop(p) #bug前面没写heapq
res.append(-val)
return res

leetcode347 Top K Frequent Elements的更多相关文章

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

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

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

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

  3. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

  4. 347. Top K Frequent Elements (sort map)

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

  5. [LeetCode] 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 ...

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

  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. [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements

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

  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. 高级T-SQL进阶系列 (二)【上篇】:使用 APPLY操作符

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 伴随着SQL SERVER 2005的发布,微软增加了一个新的操作符,它允许你将一个记录集与一个函数进行关联,然 ...

  2. 树莓派4B踩坑指南 - (8)安装GO语言

    下载 191129最新版本为go1.13.4.linux-armv6l.tar.gz go官网(点此访问)国内访问似乎不是很稳定,但下载速度还挺快,下载地址:(点此访问) 安装 解压后是一个名为go的 ...

  3. Linux 上安装 Mysql 设置root密码问题

    Ubuntu 18.10.1 Mysql 5.7.26-0 安装mysql apt-get install mysql-server 安装完可以直接使用,但是新版本在安装过程中没有提示设置root用户 ...

  4. iOS开发-真机调试遇到“The executable was signed with invalid entitlements.

    https://www.jianshu.com/p/635574a8ab0e 如果是真机运行relase版 1.Edit Scheme中改成relase 2.更改签名为   自动签名

  5. python 连接oracle基础环境配置方法

    配置基础: 1.python3.7 2.oracle server 11g 64位 3.PLSQL 64位 4.instantclient-basic-windows.x64-11.2.0.4.0这个 ...

  6. 微信小程序 列表倒计时

    最近要实现一个列表倒计时的功能,写了个demo 展示图 <view class="center colu"> <view class="time&quo ...

  7. Servlet 学习(九)

    Listener 1.功能 Servlet 2.3 中新增加的另一个功能 作用是监听Java Web 程序中的事件 对应设计模式中的Listener 模式,当事件发生的时候会自动触发该事件对应的Lis ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:可滚动

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  9. GitHub 网站汉化

    居然是一个中文Github网站!该不会是个假的吧? 2018-09-03 17:30 前几天分享了一篇文章——3个搜索技巧!在 GitHub上快速找到实用资源!眼尖心细的读者发现了文中的Github网 ...

  10. CSP-J2019 加工零件

    Background: 之前 $noip $死了,泥萌都说 \(noip SPFA\) 了,现在 \(noip\) 复活了,所以 \(SPFA\) 也复活了. (注:这里的 \(noip\) 跟 \( ...