C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm。
C#版 - Leetcode 414. Third Maximum Number题解
在线提交: https://leetcode-cn.com/problems/third-maximum-number
题目描述
414. 第三大的数
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
示例 1:
输入: [3, 2, 1]
输出: 1
解释: 第三大的数是 1.
示例 2:
输入: [1, 2]
输出: 2
解释: 第三大的数不存在, 所以返回最大的数 2 .
示例 3:
输入: [2, 2, 3, 1]
输出: 1
解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。
存在两个值为2的数,它们都排第二。
- 题目难度:
Easy - 通过次数:722
提交次数:2.6K
相关话题 数组
相似题目 数组中的第K个最大元素
思路:
使用Dictionary<int, int>存储每一个整数出现的次数,借用list对dict进行排序,然后从里面挑出第3大的KeyValuePair的Key值(出现次数为1不是必须的,不要被case 3误导)。
已AC的代码:
public class Solution
{
public int ThirdMax(int[] nums)
{
int res = 0;
Dictionary<int, int> dict = new Dictionary<int, 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.Key.CompareTo(y.Key));
if (list.Count > 2)
res = list.ElementAtOrDefault(2).Key;
else
{
res = list.First().Key;
}
return res;
}
}
Rank:
You are here!
Your runtime beats 97.53 % of csharp submissions.
ps: 刚开始使用Dictionary的dict.OrderByDescending(kv => kv.Key); 排序并没成功,后来只好使用list的sort排序了~
C#版 - Leetcode 414. Third Maximum Number题解的更多相关文章
- LeetCode 414. Third Maximum Number (第三大的数)
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- C#版 - Leetcode 633. 平方数之和 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- C#版 - Leetcode 593. 有效的正方形 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- LeetCode Array Easy 414. Third Maximum Number
Description Given a non-empty array of integers, return the third maximum number in this array. If i ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
随机推荐
- setOnTouchListener在小米手机中不走ACTION_UP而是走ACTION_CANCEL
单点触控: MotionEvent.ACTION_DOWN:手指 初次接触到屏幕 时触发. MotionEvent.ACTION_MOVE:手指 在屏幕上滑动 时触发,会多次触发. MotionEve ...
- nginx配置ssl验证
在上一篇博客中,我们只是通过nginx搭建了反向代理服务,由于需要在小程序中使用https服务,所以需要申请安全证书. 1.在所购买的域名商那申请免费的ssl证书,我买的是阿里的,所以直接在阿里上申请 ...
- BZOJ.4151.[AMPPZ2014]The Cave(结论)
BZOJ 不是很懂他们为什么都要DFS三次.于是稳拿Rank1 qwq. (三道题两个Rank1一个Rank3效率是不是有点高qwq?) 记以\(1\)为根DFS时每个点的深度是\(dep_i\).对 ...
- Android应用程序的结构和解析
什么是Android应用程序的构成? Android应用程序的各个组件又是什么? 各个组件和AndroidManifest之间的关系是什么? Android应用程序由松散耦合的组件组成,并使用应用程序 ...
- vue解决加载闪烁问题
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...
- Spring + SpringMVC + Mybatis项目中redis的配置及使用
maven文件 <!-- redis --> <dependency> <groupId>redis.clients</groupId> <art ...
- Centos7 编译测试工具 wrk bombardier iftop
1.wrk 安装及使用----------------------------------------------------------------------------------------- ...
- koa 写简单服务
这两天用koa写了点服务,这里面和express还是有部分区别的 1.静态服务: koa 中,是有中间件, koa-static, const static_f = require('koa-sta ...
- Python3小知识点
1. 是转义字符,\可以输出.在一行的末尾单独的一个\表示这行没有结束,下一行接着写 2.可以用"'或"""把一大段话引起来(可以换行)然后赋值,输出. 3.要 ...
- js 阻止事件执行
三种阻止事件执行的方式 event.preventDefault() event.stopPropagation() return false event.preventDefault() 阻止特定事 ...