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 ...
随机推荐
- 二、自动化测试平台搭建-搭建jango环境
上篇说的是安装虚拟环境,后面的项目全部放在虚拟环境上 1.创建一个虚拟环境py3,进入虚拟环境 2.安装django包:pip install django==1.8.2 3.在家目录下的Deskto ...
- tp5 mkdir(): Permission denied 问题
今天使用tp5 线上上传图片的时候遇到了一个问题 mkdir(): Permission denied 如图 百度了一下 发现大家都说 chmod -R 777 runtime 能解决问题 尝试了一下 ...
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- 我的 FPGA 学习历程(08)—— 实验:点亮单个数码管
数码管是一种常见的用于显示的电子器件,根据数码管大致可以分为共阴极和共阳极两种,下图所示的是一个共阳极的数码管的电路图(摘自金沙滩工作室的 51 开发板电路图),我的 AX301 开发板与这张图的情况 ...
- 命令行 app
[Getting Title at 38:53](http://click.pocoo.org/5/) 其实在 实际操作中 如果参数特别多的话,最好使用配置文件来操作,比如 yaml [YAML 语言 ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8
#include <iostream> #include <fstream> #include <cstdlib> const int SIZE=20; using ...
- C#调用Interrop.excel导出Excel文件失败解决方案
最近操作员反馈系统在导出Excel时失败,有抛出如下异常:系统错误信息:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失 ...
- centos为docker配置加速器
国内拉去docker镜像慢得可怜,为了解决这个问题,可为docker配置加速器. 1.修改daemon配置文件 sudo mkdir -p /etc/dockervim /etc/docker/dae ...
- Httpclient代码
/// <summary> /// 显示 /// </summary> /// <returns></returns> public ActionRes ...
- easyui-combotree选中指定的值
选中根节点: //station_id为combotree控件id var station = $('#station_id').combotree('tree').tree('getRoots'); ...