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 说明:最大线 ...
随机推荐
- HashMap扩容死循环问题
原文:https://blog.csdn.net/Leon_cx/article/details/81911223 下面我们来模拟一下多线程场景下扩容会出现的问题: 假设在扩容过程中旧hash桶中有一 ...
- Codeforces Round #555 (Div. 3) F. Maximum Balanced Circle
F. Maximum Balanced Circle 题目链接 题意 给出\(n\)个数,现在要从中选出最多的数\(b_i,b_{i+1},\cdots,b_k\),将这些数连成一个环,要求两两相邻的 ...
- template_constructor_function
#include <iostream> using namespace std; template <class T> class MyClass{ public: templ ...
- web自动化测试-模块驱动测试实例和数据驱动测试实例
一.模块驱动测试实例 把登录和退出统一封装在login类中,若把login类单独放在一个文件中,就可以给任一测试脚本调用,这里就跟测试脚本放一起 from selenium import webdri ...
- HTML 009 select_jquery操作下拉框select
取值问题 <select id="selector"> <option value="1">选项一</option> < ...
- .net core 多sdk 多版本 环境切换
在讲述.net core多版本之前,我们先理解一下.net core sdk与.net core runtime之前的联系与区别,根据官网的解释我们可以简单地理解为:sdk是在开发过程中进行使用,而r ...
- 关于H5判定区域里面滑动到底部,加载更多的总结
1.如何判定H5中滑动到底部,然后加载更多的功能实现. 思路:我们需要设定一个固定高度的盒子,然后我们利用scroll来监听滚动,当scrollTop(滚动的距离) + clientHeight(页面 ...
- 洛谷 P1012 拼数
P1012 拼数 标签 字符串 排序 NOIp提高组 1998 云端 难度 普及- 时空限制 1s / 128MB 题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例 ...
- mysql 通配符%以及_
%匹配任意长度 _ 匹配单个字符 mysql> select * from table1; +----------+------------+-----+-------------------- ...
- Alpha冲刺(5/6)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 摸鱼 准备"Alpha事后诸葛亮" 提交记录(全组共用) 接下来的计划 沟通前后端成员,监督.提醒他们 ...