leetcode347 Top K Frequent Elements
"""
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的更多相关文章
- 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 = [ ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [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 ...
- 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 ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements
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 ...
随机推荐
- 详解python的字符编码问题
一:什么是编码 将明文转换为计算机可以识别的编码文本称为“编码”.反之从计算机可识别的编码文本转回为明文为“解码”. 那么什么是明文呢,首先我们从一段信息说起,消息以人们可以理解,易懂的表示存在,我们 ...
- springboot集成过滤器
封装自定义接口filter 包含两个方法,第一个过滤的路径数组,第二个为过滤器执行的顺序.spring boot 会按照order值的大小,从小到大的顺序来依次过滤. package com.thee ...
- C/C++网络编程9——多进程服务器端实现
#include <iostream> #include <unistd.h> #include <cstdlib> #include <arpa/inet. ...
- ubuntu开启mysql远程连接,并开启3306端口
mysql -u root -p 修改mysql库的user表,将host项,从localhost改为%.%这里表示的是允许任意host访问,如果只允许某一个ip访问,则可改为相应的ip mysql& ...
- python 连接oracle基础环境配置方法
配置基础: 1.python3.7 2.oracle server 11g 64位 3.PLSQL 64位 4.instantclient-basic-windows.x64-11.2.0.4.0这个 ...
- DNS域名解析,内网
1.登录DNS服务器 windows系统 2.打开dns程序,新建区域.如下图 按默认的选项就行 2.点击空白处的右键,新建主机 具体如下图 3.可以测试访问了 ,如果做过hosts文件的修改,本机的 ...
- [转]利用 Commons-Fileupload 实现文件上传
转载 Java Web开发人员可以使用Apache文件上传组件来接收浏览器上传的文件,该组件由多个类共同组成,但是,对于使用该组件来编写文件上传功能的Java Web开发人员来说,只需要了解和使用其中 ...
- mysql之内连接,外连接(左连接,右连接),union,union all的区别
内连接,外连接,左连接,右连接,全连接 测试数据: CREATE TABLE `a_table` ( `a_id` int(11) DEFAULT NULL, `a_name` varchar(10) ...
- Mybatis的三种批量操作数据的方法
方法1: 使用for循环在java代码中insert (不推荐) 方法2: 使用 在Mapper.xml当中使用 foreach循环的方式进行insert PersonDao.java文件 publi ...
- Java基础 -4.4
For循环 for循环也是一种常规的使用结构 public static void main(String[] args) { for(定义循环的初始值;循环判断;修改循环条件) { 循环语句的执行; ...