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:
Medium
Total 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 ...
随机推荐
- Web 性能优化:21 种优化 CSS 和加快网站速度的方法
这是 Web 性能优化的第 4 篇,上一篇在下面看点击查看: Web 性能优化:使用 Webpack 分离数据的正确方法 Web 性能优化:图片优化让网站大小减少 62% Web 性能优化:缓存 Re ...
- 给树莓派开启samba服务
参考链接:https://www.cnblogs.com/mnstar/p/8144943.html 安装samba 和 samba-common-bin 启动树莓派以后,在命令行输入: sudo a ...
- 巨坑npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build/css/add.p
Windows10环境 npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build ...
- [CF364D]Ghd
[CF364D]Ghd 题目大意: 有\(n(n\le10^6)\)个数\(A_{1\sim n}(A_i\le10^{12})\),从中选取\(\lceil\frac n2\rceil\)个数,使得 ...
- Pandas 1 表格数据类型DataFrame
# -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...
- pycharm下虚拟环境建立,django项目建立等情况说明
- flagSet 使用
var ( flagSet = flag.NewFlagSet("main", flag.ExitOnError) // 参数定义 version = flagSet.Bool(& ...
- Cordova打包vue项目生成Apk (解决cordova build android抛出的zip问题)
最近对vue前端框架情有独钟.但研究了一下怎么把vue项目打包成android apk来玩玩. 首先讲一下创建vue2.x项目.其实在之前的文章中都有写过,有兴趣的同学可以去看看.http://www ...
- 推荐一些iOS博客
公司性质的: 公司 地址 美团 http://tech.meituan.com/archives 个人博客: 博主 地址 (斜体的技术文章较少) 王巍(onevcat) https://onevcat ...
- CentOS6 网络设置
由于CentOS 6默认开启了arpcheck所以在配置网卡的时候需要关闭,否则导致网络服务启动失败 [root@Mysql ~]# vi /etc/sysconfig/network-scripts ...