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 说明:最大线 ...
随机推荐
- 微信小程序和APP优劣势大对比
小程序的优势: 1. 无需下载,随走随关 2. 功能丰富,体验更简便 3. 接口众多,可以进行不断的开发 4. 流量入口大,背靠日活9.6亿的微信 5. 有强大的微信生态环境 小程序对比APP的好处: ...
- python开发笔记-ndarray方法属性详解
Python中的数组ndarray是什么? 1.NumPy中基本的数据结构 2.所有元素是同一种类型 3.别名是array 4.利于节省内存和提高CPU计算时间 5.有丰富的函数 ndarray的创建 ...
- 【python】requests 异常处理
以下是request.exceptions下的各种异常错误: RequestException: HTTPError(RequestException) UnrewindableBodyError(R ...
- Linux——配置maven
前言 Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(De ...
- Linux下TCP连接断开后不释放的解决办法
问题:在开发测试时发现断开与服务器端口后再次连接时拒绝连接. 分析:服务器上查看端口占用情况,假设端口为8888. netstat -anp |grep 8888 发现端口8888端口显示被占用(ip ...
- sql语句的面试题
中科软的面试题:http://www.mayiwenku.com/p-5888480.html 中科软的面试题:https://blog.csdn.net/woolceo/article/detail ...
- 51Node1228序列求和 ——自然数幂和模板&&伯努利数
伯努利数法 伯努利数原本就是处理等幂和的问题,可以推出 $$ \sum_{i=1}^{n}i^k={1\over{k+1}}\sum_{i=1}^{k+1}C_{k+1}^i*B_{k+1-i}*(n ...
- Classification and Decision Trees
分类和决策树(DT). 决策树是预测建模机器学习的一种重要算法. 决策树模型的表示是二叉树.就是算法和数据结构中的二叉树,没什么特别的.每个节点表示一个单独的输入变量(x)和该变量上的左右孩子(假设变 ...
- Python3中用pip离线安装
本文原创,转载请注明出处. Python3 中 离线安装 ① 生成已安装模块列表,默认存在C:\Users\Administrator下 pip freeze > requirements.tx ...
- Tensorflow细节-P312-PROJECTOR
首先进行数据预处理,需要生成.tsv..jpg文件 import matplotlib.pyplot as plt import numpy as np import os from tensorfl ...