LeetCode & Q414-Third Maximum Number-Easy
Array Math
Description:
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.
刚开始不会写,想先排序再找,发现超级麻烦,就放弃了,看了Discuss,直接用数学的方法来比较大小就好,我感觉我永远都get不到题目想让我怎么解....
public class Solution {
public int thirdMax(int[] nums) {
Integer num1 = null;
Integer num2 = null;
Integer num3 = null;
for (Integer num : nums) {
if (num.equals(num1) || num.equals(num2) || num.equals(num3)) continue;
if (num1 == null || num > num1) {
num3 = num2;
num2 = num1;
num1 = num;
} else if (num2 == null || num > num2) {
num3 = num2;
num2 = num;
} else if (num3 == null || num > num3) {
num3 = num;
}
}
return num3 == null ? num1 : num3;
}
}
LeetCode & Q414-Third Maximum Number-Easy的更多相关文章
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- LeetCode 414. Third Maximum Number (第三大的数)
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- 【leetcode】1189. Maximum Number of Balloons
题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- LeetCode_414. Third Maximum Number
414. Third Maximum Number Easy Given a non-empty array of integers, return the third maximum number ...
- 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 ...
随机推荐
- Java微服务对UTC时间格式的处理
一.背景 先说一下为什么要使用UTC时间.开发一个全球化的系统,服务端(Java微服务)集中部署在同一个地方,用户在全球通过浏览器.手机客户端访问.不同地区的时区是不一样的,同一个时间戳,不同的用户看 ...
- ES6的模块化规范和CommonJS的模块化规范的差异
ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,旨在成为浏览器和服务器通用的模块解决方案.其模块功能主要由两个命令构成:export 和 import.export命令用于规定模块的对 ...
- PPT分享 | 以太坊钱包分析与介绍
艾伯特AI人工智能(公众号:aibbtcom)按: (编者按:本文为姗姗来迟的Meetup系列推文.12月3日,林修平在亚太区以太坊社区培训与交流Meetup深圳站上跟我们分享了他对以太坊各种钱包模式 ...
- python中的str.strip()的用法
python中字符串str的strip()方法 str.strip()就是把字符串(str)的头和尾的空格,以及位于头尾的\n \t之类给删掉. 例1:str=" ABC"prin ...
- git远程提交失败
同步仓库并解决403报错 这时候对本地仓库和github进行同步 # git push -u origin master error: The requested URL returned error ...
- windows下安装mysql以及启动
配置环境变量,在path中添加 ;E:\wamp\Apache24\mysql(这是你的mysql安装路径),然后在修改一下配置文件my-default.ini(mysql安装文件夹目录下) 修改其中 ...
- 用C#开发的一个通用的地铁换乘查询工具
日常生活中,上班下班坐地铁已经是常事,每当我想去某一个远一点的地方,如果有地铁首选就是地铁,因为方便嘛!每次坐地铁,我们都是凭肉眼去得出我们心中最佳的换乘方案,但是,如果对于线路较少的城市来说,这个方 ...
- Docker自动化部署方案
一 概述 Docker发布版本应该与现有的版本发布尽量一致,参考jenkins的版本发布过程:我认为maven库和docker库有很多类似的地方,因此打包过程参考maven的打包过程:重点实现dock ...
- layui-row 布局因高度不一致错位问题
js框架为vue,通过vue去循环生成layui-col-md2;<div class="layui-row layui-col-space1"> <templa ...
- 漫谈Java IO之 NIO那些事儿
前面一篇中已经介绍了基本IO的使用以及最简单的阻塞服务器的例子,本篇就来介绍下NIO的相关内容,前面的分享可以参考目录: 网络IO的基本知识与概念 普通IO以及BIO服务器 NIO的使用与服务器Hel ...