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.

题目标签:Array

  方法1:

  题目给了我们一个nums array, 让我们找到第三大的数。其中重复的数字不算。如果是要找一个max 的数,就很简单,现在是找第三个,那么就需要维护三个max1,max2,max3。

  每次找到一个数字比 max1 大的, 或者比max2 大的,或者比max3大的,就需要重新更新三个 max 的值。

  还有一点是,这里max1 max2 max3 要用long,因为用int Integer.MIN_VALUE 的话,如果array 中有一个 是 int 里最小的数字, 那么在最后判断max3 是有数字 还是没有的情况下,就不能分辨了。

Java Solution:

Runtime beats 97.74%

完成日期:05/09/2017

关键词:Array

关键点:维护max1,max2,max3

 public class Solution
{
public int thirdMax(int[] nums)
{
long max1, max2, max3;
max2 = Long.MIN_VALUE;
max3 = max2; max1 = nums[0]; for(int i=1; i<nums.length; i++)
{
int curNum = nums[i];
if(curNum > max1) // if find the first maximum number
{
max3 = max2; // copy max2's value into max3.
max2 = max1; // copy max1's value into max2.
max1 = curNum; // copy new value into max1.
}
else if(curNum > max2) // if find the second maximum number
{
if(curNum != max1)
{
max3 = max2; // copy max2's value into max3.
max2 = curNum; // copy new value into max2.
}
}
else if(curNum > max3) // if find the third maximum number
{
if(curNum != max2)
max3 = curNum; // copy new value into max3.
}
} if(max3 == Long.MIN_VALUE) // meaning there is no third maximum value.
return (int)max1;
else
return (int)max3;
}
}

参考资料:N/A

  方法2:

  想法一样,不过运用了Integer 可以利用null,不需要把max = MIN_VALUE, 过程也比较简单清楚。

  

Java Solution:

Runtime beats 50.71%

完成日期:09/19/2017

关键词:Array

关键点:维护max1,max2,max3

 public class Solution
{
public int thirdMax(int[] nums)
{
/* Solution 2: */
Integer max1 = null;
Integer max2 = null;
Integer max3 = null; for(Integer num : nums)
{ // skip duplicate number
if(num.equals(max1) || num.equals(max2) || num.equals(max3))
continue; if(max1 == null || num > max1)
{
max3 = max2;
max2 = max1;
max1 = num;
}
else if(max2 == null || num > max2)
{
max3 = max2;
max2 = num;
}
else if(max3 == null || num > max3)
max3 = num;
} return max3 == null ? max1 : max3;
}
}

参考资料:

https://discuss.leetcode.com/topic/63715/java-neat-and-easy-understand-solution-o-n-time-o-1-space

LeetCode 题目列表 - LeetCode Questions List

LeetCode 414. Third Maximum Number (第三大的数)的更多相关文章

  1. 414 Third Maximum Number 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...

  2. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  3. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  4. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  5. Leetcode414Third Maximum Number第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  6. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

  7. 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...

  8. 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 ...

  9. 414. Third Maximum Number数组中第三大的数字

    [抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...

随机推荐

  1. Python爬虫总结

    Python爬虫的原理:1通过URLopen()来获取到url页面, 这个过程可以加代理 2这个页面上都是字符串,所以我们而通过字符串查找的方法来获取到目标字符串,用到了正则来匹配目标re.finda ...

  2. Struts2配置文件复用代码【web.xml、struts.xml、常量配置】

    web.xml的分发器代码: <!-- 引入struts核心过滤器 --> <filter> <filter-name>struts2</filter-nam ...

  3. “AOP代理”遇到“双上下文”

    最近有小伙伴儿遇到了一个问题来咨询我,问题大致如下: 他在Service层利用Aspect设置了一个Spring AOP代理,在单元测试以及在service层代码上添加代理的时候均没有发现问题,但是在 ...

  4. Java学习笔记一---JVM、JRE、JDK

    jdk包含jre,jre包含jvm. 用java语言进行开发时,必须先装jdk: 只运行java程序,不进行开发时,可以只装jre. JVM 即Java Virtual machine,Java虚拟机 ...

  5. EntityFramework 6.x多个上下文迁移实现分布式事务

    前言 自从项目上了.NET Core平台用上了EntityFramework Core就再没碰过EntityFramework 6.x版本,目前而言EntityFramework 6.x是用的最多,无 ...

  6. Android性能优化xml之<include>、<merge>、<ViewStub>标签的使用

    一.使用<include>标签对"重复代码"进行复用 <include>标签是我们进行Android开发中经常用到的标签,比如多个界面都同样用到了一个左侧筛 ...

  7. vue-ajax小封装

    1. js 文件: /** ajax封装:* 1. 引入文件* 2. new Vue().ajax.get(url,data,fn,ojson), 或 new Vue().ajax.post(url, ...

  8. 再起航,我的学习笔记之JavaScript设计模式27(链模式)

    链模式 概念介绍 链模式(Operatc of Responsibility): 通过在对象方法中将当前对象返回,实现对同一个对象多个方法的链式调用.从而简化对该对象的多个方法的多次调用时,对该对象的 ...

  9. [VirtualBox] 1、NAT模式下端口映射

    1.VirtualBox中有4中网络连接方式 VirtualBox中有4中网络连接方式:NAT.Bridged Adapter.Internal.Host-only Adapter,VMWare中有三 ...

  10. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...