C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm。
C#版 - Leetcode 347. Top K Frequent Elements - 题解
在线提交: 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:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
Difficulty:
MediumTotal Accepted: 109.7K
Total Submissions: 219.6K
Related Topics Hash TableHeap
Similar Questions Word FrequencyKth Largest Element in an ArraySort Characters By FrequencySplit Array into Consecutive SubsequencesTop K Frequent Words
思路:
使用字典Dictionary<int, int>存储每个数出现的次数,并对次数进行排序。有两种方法;
1.将Dictionary转为List,实现Sort的CompareTo方法;
2.使用Dictionary的OrderByDescending(f => f.value),并Take(k)。
由于转换为List后进行排序会让程序速度变慢,建议直接用后一种方法。
已AC代码:
public class Solution
{
public IList<int> TopKFrequent(int[] nums, int k)
{
var dict = new Dictionary<int, int>();
IList<int> result = new List<int>();
foreach (var num in nums)
{
if (!dict.ContainsKey(num))
dict.Add(num, 1);
else dict[num]++;
}
var list = dict.ToList();
list.Sort((x, y) => -x.Value.CompareTo(y.Value));
if (list.Count >= k)
{
for (int i = 0; i < k; i++)
{
result.Add(list.ElementAtOrDefault(i).Key);
}
}
return result;
}
}
改进版:
public class Solution
{
public IList<int> TopKFrequent(int[] nums, int k)
{
var dict = new Dictionary<int, int>();
IList<int> result = new List<int>();
foreach (var num in nums)
{
if (!dict.ContainsKey(num))
dict.Add(num, 1);
else dict[num]++;
}
var list = dict.OrderByDescending(f => f.Value).Take(k).ToList();
for (int i = 0; i < k; i++)
result.Add(list.ElementAtOrDefault(i).Key);
return result;
}
}
Rank:
You are here!
Your runtime beats 99.28 % of csharp submissions.
C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解的更多相关文章
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
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 ...
- [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 ...
- [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 ...
- Java [Leetcode 347]Top K Frequent Elements
题目描述: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...
- C#版[击败99.69%的提交] - Leetcode 242. 有效的同构异形词 - 题解
C#版 - Leetcode 242. 有效的同构异形词 - 题解 Leetcode 242.Valid Anagram 在线提交: https://leetcode.com/problems/val ...
- 【leetcode】347. Top K Frequent Elements
题目地址:https://leetcode.com/problems/top-k-frequent-elements/ 从一个数组中求解出现次数最多的k个元素,本质是top k问题,用堆排序解决. 关 ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
随机推荐
- fiddler安装 与 https
1.下载最新版fiddler ,强烈建议在官网下载:https://www.telerik.com/download/fiddler 2. 正常傻瓜式安装,下一步,下一步,安装完毕后,先不用急于打开软 ...
- 极速创建 IOS APP !涛舅舅苹果 IOS APP自助生成系统正式上线
经过大量的测试和开发工作,涛舅舅苹果 IOS APP自助生成系统正式上线! 本系统主要功能: 1.用最最简单的方式将H5网站打包生成一个苹果APP 2.只需要提供APP标题,H5网站首页url地址,一 ...
- Spring保护方法
Spring保护方法 一.使用注解保护方法 1.@Secured 由Spring Security提供,首先需要启用基于注解的方法安全性: @EnableGlobalMethodSecurity(se ...
- WebStorm重复代码快捷表达
一,问题 平时使用WebStorm时需要很多引用js,重复代码比较多,每次都要写很多次同样的代码,那么如何通过几个快捷键来简单的写出重复代码呢? 问题具体描述: 每次都要写两个script的重复: & ...
- android 第一次作业
天气预报界面截图: 源码coding地址:https://coding.net/u/dsy1600802076/p/android/git/tree/master
- Tesseract-ocr 工具使用记录
1.很多地方可以下载Tesseract-ocr工具,同时下载jTessBoxEditor方便之后的训练使用 2.下载Tesseract-ocr后可以放在任何地方,但是需要设置环境变量,或者在CMD当中 ...
- php获取当前时间的毫秒数
floor(microtime()*1000); 用microtime能输出当前的秒的后面8位小数 乘以1000取整数就行了
- Github把自己的本地项目托管到git上
开篇之前说下题外话,之前写过一篇博客,IOS-一步一步教你自定义评分星级条RatingBar,群里有人想要源码,我上传到github上了,有需要的可以去看一下,github地址自定义评分星级条 言归正 ...
- RabbitMQRPC 官方demo
public class RPCServer { public static void Test() { var factory = new ConnectionFactory() { HostNam ...
- 201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
实验十二 图形程序设计 实验时间 2018-11-14 1.实验目的与要求 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: 创建空框架 . 在Java中,常采用框架(Frame) ...