LeetCode_414. Third Maximum Number
414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example 1:
Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1.
Example 2:
Input: [1, 2] Output: 2 Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1] Output: 1 Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
package leetcode.easy; public class ThirdMaximumNumber {
@org.junit.Test
public void test() {
int[] nums1 = { 3, 2, 1 };
int[] nums2 = { 1, 2 };
int[] nums3 = { 2, 2, 3, 1 };
System.out.println(thirdMax(nums1));
System.out.println(thirdMax(nums2));
System.out.println(thirdMax(nums3));
} public int thirdMax(int[] nums) {
int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
boolean minPresent = false;
for (int n : nums) {
if (n == Integer.MIN_VALUE) {
minPresent = true;
} if (n > max1) {
max3 = max2;
max2 = max1;
max1 = n;
} else if (n > max2 && n < max1) {
max3 = max2;
max2 = n;
} else if (n > max3 && n < max2) {
max3 = n;
} }
if (max3 != Integer.MIN_VALUE) {
return max3;
} else {
if (minPresent && max2 != Integer.MIN_VALUE) {
return max3;
} else {
return max1;
}
}
}
}
LeetCode_414. Third Maximum Number的更多相关文章
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- Failed to connect to database. Maximum number of conections to instance exceeded
我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...
- POJ2699 The Maximum Number of Strong Kings
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2102 Accepted: 975 Description A tour ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080
1.INFO: Maximum number of threads (200) created for connector with address null and port 8091 说明:最大线 ...
随机推荐
- C# CustomValidator
当各种验证控件的验证类型都不能满足需要时可以使用CustomValidator验证控件,通过自定义验证函数来验证.直接看例子 <%@ Page Language="C#" A ...
- GO语言开发之路
Go语言开发之路 介绍 为什么学习Go语言? 开发环境准备 从零开始搭建Go语言开发环境 VS Code配置Go语言开发环境 基础 Go语言基础之变量和常量 Go语言基础之基本数据类型 Go语言基础之 ...
- Spring源码窥探之:扩展原理BeanFactoryPostProcessor
BeanPostPorcessor是在bean创建对象初始化前后进行拦截工作,而BeanFactoryPostProcessor是Bean工厂的后置处理器,在Bean定义加载完成之后,Bean实例初始 ...
- 当调用对象中不存的方法、属性时,__getattr__的应用场景
一.Python中创建类和实例的调用顺序 new(cls) 创建对象前调用,如果类中没定义,会一直向父类找,直到object的 new 方法创建类.cls代表类本身 init(self) 创建类实例后 ...
- Generator 函数和for...of循环,实现斐波那契数列
function* fib () { let [prev, cur] = [0,1] for (;;) { yield cur [prev, cur] = [cur, cur+prev] } } fo ...
- jaeger使用yugabyte作为后端存储的尝试以及几个问题
前边写过使用scylladb 做为jaeger 的后端存储,还是一个不错选择的包括性能以及 兼容性,对于 yugabyte 当前存在兼容性的问题,需要版本的支持,或者尝试进行一些变动 create 语 ...
- 在AD中设置漫游配置文件与文件夹重定向
在域环境下,域用户可以在域中的任意一台客户端计算机上登录,由于普通域用户的权限比较低,在大多数情况下只能对自己的用户配置文件具有完全控制权限,因而大多数域用户都是将数据直接保存在用户配置文件中.用户配 ...
- 浅谈SPFA判负环
目录 SPFA判负环 [前言] [不可代替性] [具体实现] SPFA的过程 判负环 [核心代码] [例题] SPFA判负环 有不足的地方请指出 本蒟蒻一定会修改吼 [前言] 最短路的求法中最广为人知 ...
- 【POJ1068】Parencodings
题目传送门 本题知识点:模拟 这是一道恐怖的括号题.题意稍微理解以下还是可以的. 我们针对样例来理解一下 S.P.W 到底是什么意思: S:( ( ( ( ) ( ) ( ) ) ) ) P: \(P ...
- 无法定位程序输入点到xxx.dll
Q:安装pytorch时报错无法定位程序输入点到Anaconda3\Library\bin\libssl-1_1-x64.dll A:下载libssl-1_1-x64.dll覆盖bin下的文件 下载地 ...