414 Third Maximum Number 第三大的数
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
示例 1:
输入: [3, 2, 1]
输出: 1
解释: 第三大的数是 1.
示例 2:
输入: [1, 2]
输出: 2
解释: 第三大的数不存在, 所以返回最大的数 2 .
示例 3:
输入: [2, 2, 3, 1]
输出: 1
解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。
存在两个值为2的数,它们都排第二。
详见:https://leetcode.com/problems/third-maximum-number/description/
Java实现:
class Solution {
public int thirdMax(int[] nums) {
long first=Long.MIN_VALUE;
long second=Long.MIN_VALUE;
long third=Long.MIN_VALUE;
for(int num:nums){
if(first<num){
third=second;
second=first;
first=num;
}else if(num>second&&num<first){
third=second;
second=num;
}else if(num>third&&num<second){
third=num;
}
}
return (third==Long.MIN_VALUE||third==second)?(int)first:(int)third;
}
}
C++实现:
class Solution {
public:
int thirdMax(vector<int>& nums) {
long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN;
for (int num : nums)
{
if (num > first)
{
third = second;
second = first;
first = num;
}
else if (num > second && num < first)
{
third = second;
second = num;
}
else if (num > third && num < second)
{
third = num;
}
}
return (third == LONG_MIN || third == second) ? first : third;
}
};
参考:https://www.cnblogs.com/grandyang/p/5983113.html
414 Third 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 ...
- Leetcode414Third Maximum Number第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- 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 ...
- 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 ...
- 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 ...
- [Array]414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
随机推荐
- onfocus事件,onblur事件;Focus()方法,Blur()方法
<1> <pre name="code" class="html"><!DOCTYPE html PUBLIC "-// ...
- js实现的美女瀑布流效果代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Visual Studio Code 常用插件
这里记录在Visual Studio Code中,我经常用到的插件. C#这个就不必说了,想用VIsual Studio Code调试和编辑C#代码,必须要装这个插件.东西还好,就是每次安装和更新插件 ...
- centos7 rpm 安装MySQL5.6
系统centos7,安装MySQL,出错,百度了一大堆,几乎都是通过yum安装的MySQL,我想说的是,通过yum自动安装的话系统自己会帮你做很多事情,相关联的包也会下下来,相当于替你省去了很多事情, ...
- 基于开源项目的在线网络视频直播项目---pc端的推流
https://github.com/winlinvip/simple-rtmp-server/issues/66 https://github.com/justinmakaila/iOS-Frame ...
- Error 99 connecting to 192.168.3.212:6379. Cannot assign requested address
Error 99 connecting to 192.168.3.212:6379. Cannot assign requested address Redis - corelation betwee ...
- Class.forName() 详解
主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...
- stl之vector的应用
这里主要是对vector容器的一些常见应用的总结.至于vector的构造函数及初始化能够參考http://blog.csdn.net/lsh_2013/article/details/21191289 ...
- UVALive3126 Taxi Cab Scheme —— 最小路径覆盖
题目链接:https://vjudge.net/problem/UVALive-3126 题解: 最小路径覆盖:即在图中找出尽量少的路径,使得每个结点恰好只存在于一条路径上.其中单独一个点也可以是一条 ...
- bzoj1858 [Scoi2010]序列操作——线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1858 线段树...调了一个上午...(后面带 // 的都是改出来的) lazy 标记的下放好 ...