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的更多相关文章

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

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

  2. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

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

  4. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  5. LeetCode 321. Create Maximum Number

    原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...

  6. 【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 ...

  7. LeetCode 414 Third Maximum Number

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

  8. 【leetcode】414. Third Maximum Number

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

  9. LeetCode_414. Third Maximum Number

    414. Third Maximum Number Easy Given a non-empty array of integers, return the third maximum number ...

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

随机推荐

  1. 论文笔记(1):From Image-level to Pixel-level Labeling with Convolutional Networks

    文章采用了多实例学习(MIL)机制构建图像标签同像素语义的关联 . 该方法的训练样本包含了70 万张来自ImageNet的图片,但其语义分割的性能很大程度上依赖于复杂的后处理过程,主要包括图像级语义的 ...

  2. JAVA通过COM接口操作PPT

    一. 背景说明 在Eclipse环境下,开发JAVA代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,即先收工创建好模板,在程序中修改模板数据. ...

  3. Nancy启用跨站攻击防护(CSRF)

    什么是CSRF(跨站攻击) 可能很多人已经对CSRF有所了解,就简单的介绍下: CSRF全程是 Cross-Site Request Forgery .大概意思就是在登录用户不知情的情况下,由一个网站 ...

  4. which framework or library is best to use WebRTC

    which framework or library is best to use WebRTC http://stackoverflow.com/questions/24857637/current ...

  5. JDK的安装和Java环境变量配置

    所需工具:JDK    下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html

  6. unity A*寻路 (一)导出NavMesh数据

    使用unity的API   NavMesh.CalculateTriangulation 可以获取NavMesh数据 首先 我们创建一个新的工程 保存一个test场景 然后在场景中添加一个Plane作 ...

  7. 20165230 2017-2018-2 《Java程序设计》第3周学习总结

    20165230 2017-2018-2 <Java程序设计>第3周学习总结 教材学习内容总结 本周主要学习了类与对象. 包括创建对象与构造方法. 了解了程序是由若干个类所构成:类分为类名 ...

  8. async generator promise异步方案实际运用

    es7 async方案 /******************async***********************/ var timeFn=function(time){ return new P ...

  9. IDEA设置生成类基本注释信息

    在eclipse中我们按一下快捷键就会生成类的基本信息相关的注释,其实在IDEA中也是可以的,需要我们手动设置,之后再创建类的时候就会自动加上这些基本的信息. File-->Setting 在E ...

  10. [Tarjan 学习笔记](无向图)

    今天考试因为不会敲 Dcc 的板子导致没有AK(还不是你太菜了),所以特地写一篇博客记录 Tarjan 的各种算法 无向图的割点与桥 (各种定义跳过) 割边判定法则 无向边 (x,y) 是桥,当且仅当 ...