"""
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. "%Error opening tftp://255.255.255.255/network config"

    问题:服务配置错误消息(Service Configuration Error Messages) 有时,在通过Cisco IOS软件启动Cisco设备期间,会显示与这些类似的错误消息: %Error ...

  2. ISR4K-IOS XE EPC

    1.该操作在ISR4K的平台操作,简单的执行了一个控制层面的抓包 配置命令: R01#monitor capture A control-plane both R01#monitor capture ...

  3. 常用的OpenCV 2.0函数速查

    OpenCV 2.0函数释义列表 1.cvLoadImage:将图像文件加载至内存: 2.cvNamedWindow:在屏幕上创建一个窗口: 3.cvShowImage:在一个已创建好的窗口中显示图像 ...

  4. Python实现一个LRU

    class Node: key = None value = None pre = None next = None def __init__(self, key, value): self.key ...

  5. 编写自己的JDBC框架

    目的 简化代码,提高开发效率 设计模式 策略设计模式 代码 #连接设置 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost ...

  6. ES的基本概念

    elasticsearch 的索引与文档是开发关注的视角:节点.集群.分片是运维关注的视角 elasticearch 文档的介绍 - elasticearch 是面向文档的,文档是所有可搜索数据的最小 ...

  7. java后端开发echarts

    参考: https://blog.csdn.net/mxdmojingqing/article/details/77340245 https://github.com/abel533/ECharts

  8. 【PAT甲级】1017 Queueing at Bank (25 分)

    题意: 输入两个正整数N,K(N<=10000,k<=100)分别表示用户的数量以及银行柜台的数量,接下来N行输入一个字符串(格式为HH:MM:SS)和一个正整数,分别表示一位用户到达银行 ...

  9. Android学习笔记17:单项选择RadioButton和多项选择CheckBox的使用

    请参见 http://www.android100.org/html/201406/05/19495.html

  10. Nodejs回调加超时限制两种实现方法

    odejs回调加超时限制两种实现方法 Nodejs下的IO操作都是异步的,有时候异步请求返回太慢,不想无限等待回调怎么办呢?我们可以给回调函数加一个超时限制,到一定时间还没有回调就表示失败,继续后面的 ...