LeetCode 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.
题目标签: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 (第三大的数)的更多相关文章
- 414 Third Maximum Number 第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- Leetcode414Third Maximum Number第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- 【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换最大值数组 使用set 三个变量 日期 题目地址 ...
- 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 ...
- 414. Third Maximum Number数组中第三大的数字
[抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...
随机推荐
- linux下svn命令大全(转)
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain 简写:s ...
- 生成验证码JSP【复用代码】
该JSP可以生成验证码.以后用到的时候就方便了. <%@ page language="java" pageEncoding="UTF-8"%> & ...
- C++中const几中用法
转载自:http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html 1. const修饰普通变量和指针 const修饰变量,一般 ...
- 框架基础:ajax设计方案(六)--- 全局配置、请求格式拓展和优化、请求二进制类型、浏览器错误搜集以及npm打包发布
距离上一次博客大概好多好多时间了,感觉再不搞点东西出来,感觉就废了的感觉.这段时间回老家学习驾照,修养,然后7月底来上海求职(面了4家,拿了3家office),然后入职同程旅游,项目赶进度等等一系列的 ...
- Nodejs最好的ORM - TypeORM
TypeORM是一个采用TypeScript编写的用于Node.js的优秀ORM框架,支持使用TypeScript或Javascript(ES5, ES6, ES7)开发.目标是保持支持最新的Java ...
- 入坑IT都快十年了
一起帮的开发直播已经告一段落:一是主体的功能差不多都实现了,二是用到的架构技术都展示得差不多了.以后就算继续开发,也应该都是一些“技术上”重复的工作而已.整个直播过程耗时近半年,SVN提交1062次, ...
- CA认证和颁发吊销证书
摘要:涉及到网络安全这一块,想必大家都听过CA吧.像百度.淘宝.京东等这些知名网站,每年都要花费一笔money来买CA证书.但其实简单的企业内的CA认证,我们自己就可以实现,今天小编我就讲解一下怎么在 ...
- TComboBox组件重要属性和事件
TComboBox组件的重要属性 CharCase--------此属性用于设置编辑框内文字的大小写 DropDownCount---此属性用于设置当用户下拉组合框时不需要加滚动条就能显示的项的个数 ...
- 【转】Keberos认证原理
前几天在给人解释Windows是如何通过Kerberos进行Authentication的时候,讲了半天也别把那位老兄讲明白,还差点把自己给绕进去.后来想想原因有以下两点:对于一个没有完全不了解Ker ...
- Linux入门之常用命令(2)
(三) 链接文件 ln [-s] [源文件] [目标文件] -s表示符号链接 没有则是硬链接 硬链接是一个独立文件 (相当于一个副本) 符号链接是一个链接文件(相当于一个快捷方式) 但是修 ...