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 说明:最大线 ...
随机推荐
- 矩阵指数 Matrix Exponentials
转自:https://zh.wikipedia.org/wiki/%E7%9F%A9%E9%98%B5%E6%8C%87%E6%95%B0 其中,X. X2.X3…….Xk 都是n阶矩阵,显然 exp ...
- 小白式Git使用教程,从0到1
Git是什么? Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remo ...
- Python - 2和3的区别
编码: Python2的默认编码是ASCII码,这是导致Python2中经常遇到编码问题的主要原因之一,至于原因,在于Python这门语言出现的时候,还没有Unicode! Python3默认编码是U ...
- C#操作域用户ADHelper
在C#中操作域用户,在项目中写的帮助类: using System; using System.Collections.Generic; using System.DirectoryServices; ...
- luoguP1120小木棍(POJ - 1011 )
题意: 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50,个数不超过65. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段 ...
- K-means:如何选择K(cluster的数目)
目前决定cluster数目的常用方法是手动地决定cluster的数目 哪个K是正确的? 上图中的数据集,我们可以说它有4个clusters,也可以说它有2个clusters,但哪个是正确答案呢?其实这 ...
- SpringBoot源码分析-编译环境与新建测试模块
建议 分析源码建议不要使用Idea或者Eclipse等IDE工具的反编译功能或者导入源码包的方式看源码,那样不能给框架的源码做注释,所以分析源码之前都得先下载源码并构建,然后在项目中新建一个Modul ...
- python - 手机号正则匹配
Python 手机号正则匹配 # -*- coding:utf-8 -*- import re def is_phone(phone): phone_pat = re.compile('^(13\d| ...
- BZOJ 5507: [gzoi2019]旧词 LCT
和之前那个 [LNOI]LCA 几乎是同一道题,就是用动态树来维护查分就行. code: #include <bits/stdc++.h> using namespace std; #de ...
- CLR 调试概述
利用公共语言运行时 (CLR) 调试 API,工具供应商可以编写调试器来调试运行于 CLR 环境中的应用程序. 要调试的代码可为 CLR 支持的任何代码种类.CLR 调试 API 主要是使用非托管代码 ...