Leetcode414Third Maximum Number第三大的数
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
示例 1:
输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1.
示例 2:
输入: [1, 2] 输出: 2 解释: 第三大的数不存在, 所以返回最大的数 2 .
示例 3:
输入: [2, 2, 3, 1] 输出: 1 解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。 存在两个值为2的数,它们都排第二。
class Solution {
public:
int thirdMax(vector<int>& nums) {
int first = -2147483648;//INT_MIN
int second = -2147483648;
int third = -2147483648;
int cnt = 0;
map<int, int> visit;
for(int i = 0; i < nums.size(); i++)
{
if(visit[nums[i]] != 0)
continue;
cnt++;
if(nums[i] > first)
{
third = second;
second = first;
first = nums[i];
}
else if(nums[i] > second)
{
third = second;
second = nums[i];
}
else if(nums[i] > third)
{
third = nums[i];
}
visit[nums[i]]++;
}
if(cnt > 2)
return third;
else
return first;
}
};
Leetcode414Third Maximum Number第三大的数的更多相关文章
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- 414 Third Maximum Number 第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...
- 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 ...
- [Swift]LeetCode414. 第三大的数 | 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) 3
414. 第三大的数 414. Third Maximum Number 题目描述 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是 O(n). 每 ...
- C#LeetCode刷题之#414-第三大的数(Third Maximum Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...
- ORA-00018: maximum number of sessions exceeded 超出最大会话数
ORA-00018: maximum number of sessions exceededORA-00018: 超出最大会话数 Cause: All session state obje ...
- 414. Third Maximum Number数组中第三大的数字
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
随机推荐
- Luogu P2717 寒假作业(平衡树)
P2717 寒假作业 题意 题目背景 \(zzs\)和\(zzy\)正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有\(n\)项寒假作业.\(zzy\)给每项寒假作业都定义了一个疲劳值 ...
- Python全栈开发:css引入方式
css的四种引入方式: 1.行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用. <p style="color: red;backgr ...
- Java jmx的使用
JMX Java Management Extensions,Java管理扩展.本质就是用来监控java语言开发的程序,一般常用于jconsole,java visual VM的监控,今天主要介绍ja ...
- H5在部分苹果手机IOS系统下重力感应无效
原因不明,反正在IOS系统12.2以上的普通的H5移动端网页的重力感应功能无反应 解决方法: 把链接改为HTTPS,经过测试,12.2系统以上的如果你的链接是HTTP的,重力感应在网页上是没调起的,改 ...
- 牛客网在线判题系统JavaScript(V8)使用
JavaScript作为一种弱类型的编程语言,语法和C/C++.JAVA等存在差别,但是对于大部算法题,不只是C/C++.JAVA,也依然可以使用JavaScript来实现.所以在牛客网中,如果你喜欢 ...
- 菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)[转]
菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...
- Error-Idea:Process finished with exit code 1
ylbtech-Error-Idea:Process finished with exit code 1 1.返回顶部 1. log4j:WARN No appenders could be foun ...
- Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete 1.返回顶部 1. insert, update 和 delete 数据变更 ...
- 《DSP using MATLAB》Problem 8.25
用match-z方法,将模拟低通转换为数字低通 代码: %% --------------------------------------------------------------------- ...
- java基础之final关键字
final: 意为终态.在java中得注意以下四点: 1.final是一个修饰符,可修饰变量,方法,类. 2.final修饰子类不可以被继承. 3.final修饰的方法不可以被重写(覆盖) 4.对于一 ...