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. 关于在jsp页面中使用/struts-tags标签库的迭代器时,从ValueStack获取参数使用EL的问题

    情况复原下: 通过valuestack存了个user,然后页面里可以${user.sex}拿到男.${user.sex=='男'}拿到ture 但是在使用 <s:if text="${ ...

  2. 【Java】关于Java8 parallelStream并发安全的思考

    背景 Java8的stream接口极大地减少了for循环写法的复杂性,stream提供了map/reduce/collect等一系列聚合接口,还支持并发操作:parallelStream. 在爬虫开发 ...

  3. jenkins~集群分发功能的具体实现

    前一讲主要说了jenkins分发的好处<jenkins~集群分发功能和职责处理>,它可以让具体的节点干自己具体的事,比如windows环境下的节点,它只负责编译,发布windows的生态环 ...

  4. [spring 入门第一天]

    关于Spring Framework 简介: Spring框架提供了一个全面的现代java企业应用程序编程和配置模型——可以部署在任何类型的平台.支持任何级别的应用程序:spring专注于程序架构,这 ...

  5. Android Studio 字体和字号调整

    点击File,Settings. 找到Editor-Colors&Fonts-Font 点击Save As... 改个名字点击OK. 1为字体,2为字号,3为行间距. 我认为字体设置为Cons ...

  6. Another Easy Problem fzu1753

    Another Easy Problem Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u ...

  7. hdu3018欧拉回路题

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. S2_OOP第二章

    第一章 继承 语法 修饰符 子类 extends 父类{ //类定义不封 } 使用extends继承父类的属性和方法.使用super关键字调用父类的方法. 概念 继承是面向对象的三大特特之一,Java ...

  9. 在Java环境上运行redis

    首先你得有Java环境,不多说,参考http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.html 下载redis驱动包 链接:http: ...

  10. 【原创】流程引擎的网关(遵循BPMN2.0)设计总结

    概述 BPMN 2.0是什么呢?业务流程模型注解(Business Process Modeling Notation - BPMN)是 业务流程模型的一种标准图形注解.这个标准 是由对象管理组(Ob ...