leetcode347
public class Solution {
public IList<int> TopKFrequent(int[] nums, int k) {
var dic = new Dictionary<int, int>();
foreach (var num in nums)
{
if (!dic.ContainsKey(num))
{
dic.Add(num, );
}
else
{
dic[num]++;
}
}
var list = dic.OrderByDescending(x => x.Value).ToList();
var listR = new List<int>();
for (int i = ; i < k; i++)
{
listR.Add(list[i].Key);
}
return listR;
}
}
https://leetcode.com/problems/top-k-frequent-elements/#/description
补充一个python的实现,使用封装好的类库,代码很简洁。
import collections
import heapq class Solution:
def topKFrequent(self, nums, k):
count = collections.Counter(nums)
return heapq.nlargest(k, count.keys(), key=count.get)
leetcode347的更多相关文章
- LeetCode347:返回频率前K高的元素,基于优先队列实现
package com.lt.datastructure.MaxHeap; import java.util.LinkedList; import java.util.List; import jav ...
- [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- leetcode347. 前 K 个高频元素
题目最终需要返回的是前 kk 个频率最大的元素,可以想到借助堆这种数据结构,对于 kk 频率之后的元素不用再去处理,进一步优化时间复杂度. 具体操作为: 借助 哈希表 来建立数字和其出现次数的映射,遍 ...
- LeetCode347——优先队列解决查询前k高频率数字问题
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 例如, 给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]. 注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- leetcode347 Top K Frequent Elements
""" Given a non-empty array of integers, return the k most frequent elements. Example ...
- leetcode347 python
通过维护最小堆排序,使用heapq模块 一般使用规则:创建列表 heap = [] 函 数 ...
- leetcode347 —— n中topK && PriorityQueue(Heap) && Map遍历
题目要求:求前K个最频繁出现的数字. 1.很容易想到,使用HashMap<Integer,Integer>来存储<number,frequency>键值对 1 int n = ...
- [Swift]优先队列PriorityQueue(自定义数据结构)
优先队列[priority queue] 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除. 优先队列特点:在优先队列中,元素被赋予优先级. 当访问元素时,具有最高优先级的元素最先 ...
- [Swift]实现优先队列PriorityQueue
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
随机推荐
- angular2升级到angular4历程
Angular 4 在昨天(2017-03-24)正式发布了,我的系列教程也得更新一下.步骤略繁琐,不用 cli 的项目反倒更简单一些,但是 cli 平时给我们的便利还是很多的,升级最多半年一次而已. ...
- springcloud Ribbon学习笔记二
之前介绍了如何搭建eureka服务并开发了一个用户服务成功注册到了eureka中,接下来介绍如何通过ribbon来从eureka中获取用户服务: springcloud ribbon提供客户端的负载均 ...
- input做一个开关按钮
.mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-c ...
- wpf 控件简单介绍
- C# 引用类型公共变量的影响
public int[] a =new int[2]; private void button1_Click(object sender, EventArgs e) { bing(a); } priv ...
- cocos2dx开发之util类&方法——字符串替换
/*将originStr字符串中的searchStr替换成replaceStr*/ std::string str_replace(std::string originStr,std::string ...
- day02python 整型 布尔
今日内容 int bool 详细内容 1.整型(int) Py2 32位电脑 64位电脑 超出范围后python将自动转换成long(长整型) /运算不能显示小数-> (整形除法只能保留整数位) ...
- message box
QMessageBox 弹出框上的按钮设置为中文 Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::War ...
- 2018.5.2 file结构体
f_flags,File Status Flag f_pos,表示当前读写位置 f_count,表示引用计数(Reference Count): dup.fork等系统调用会导致多个文件描述符指向同一 ...
- JDK下载与安装、 Eclipse下载与使用、 Tomcat下载与使用、 MySQL安装与使用
前言 本文将介绍JDK的下载与安装,eclipse的下载与使用,Tomcat的下载与使用,MySQL的安装与使用. JDK下载与安装 一.JRE与JDK介绍 java是当前比较流行的一种编程语言,当我 ...