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. oracle-获取数据库中所有表的注释 comments

    公司dba提供的脚本: set serveroutput on set feedback off spool /tmp/getcomments.out select 'comment on table ...

  2. Activiti-02-activiti api

    流程引擎API和服务 通过ProcessEngine你可以获取各种服务,它和所有的服务对象都是线程安全的,因此整个整个应用中可以只有一份. ProcessEngine processEngine =P ...

  3. Hibernate4+EhCache配置二级缓存

    本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...

  4. iOS多线程编程

    废话不多说,直接上干货.先熟悉一下基本知识,然后讲一下常用的两种,NSOperation和GCD. 一.基础概念 进程: 狭义定义:进程是正在运行的程序的实例(an instance of a com ...

  5. Dynamic web module 版本之间的区别

    Servlet 3十二月2009开发平台标准版6,6可插性,易于开发,异步ser vlet,安全,文件上传Servlet 2.5九月2005开发平台标准版5,5需要平台标准版5,支持注释Servlet ...

  6. 双击打开Jar文件

    最近发现个诡异的问题,java环境变量明明配好了.但是双击xx.jar文件,就是不能直接打开运行. 先想到了第一个解决办法: 运行cmd.exe,cd到jar目录,执行 javaw -jar xxx. ...

  7. WPA/WPA2加密破解

    WPA/WPA2无线密码破解这里主要介绍两种方法:穷举PIN码.穷举密码 穷举PIN码(共11000种组合)破解速度快,需要一些条件(路由器支持WPS.QSS功能),路由器信号良好.无论密码多复杂,条 ...

  8. 关于SSH

    SSH的英文全称是Secure Shell. 传统的网络服务程序,如:ftp和telnet在本质上都是不安全安全安全安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令 ...

  9. netty4.x 传输文件

    一:简介 netty传输文件的例子并不多,当前的项目刚才需要使用netty,所以就记录一下使用方法,使用netty传输文件,首先需要启动一个服务端,等待服务端请求监听,然后传输文件的时候,启动一个客户 ...

  10. Google赛马分析

    原题 想必田忌赛马的故事,大家都耳熟能详.但是,大家知道Goolge的童鞋们是怎么赛马的么?不过,首先,大家要先尝试一下:有25匹马,每次只能五匹一起跑,那么最少跑几次,才能确定前三甲呢? 分析 这样 ...