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.被处理的数据量级比较大.没有明显规律可循,可以分割.或者提取文件,采用分步和保存思想去解决. 比如在使用python的正则re模块时候,根据被处理对象提取属性抽象一个基类,方法实现分步,并且把中间 ...

  2. C#将.spl剥离成.emf文件格式

    本文转载自 星战紫辉 http://www.cppblog.com/rawdata/archive/2009/02/23/74653.html 但C#代码实现为本人原创.https://github. ...

  3. Tensorflow小技巧整理:修改张量特定元素的值

    TensorFlow小技巧整理:修改张量特定元素的值 最近在做一个摘要生成的项目,过程中遇到了很多小问题,从网上查阅了许多别人解决不同问题的方法,自己也在旁边开了个jupyter notebook搞些 ...

  4. iOS 提交审核报错 ERROR ITMS-90087解决办法

    ERROR ITMS-: "Unsupported Architectures. The executable for yht.temp_caseinsensitive_rename.app ...

  5. python 装饰器 一篇就能讲清楚

    装饰器一直是我们学习python难以理解并且纠结的问题,想要弄明白装饰器,必须理解一下函数式编程概念,并且对python中函数调用语法中的特性有所了解,使用装饰器非常简单,但是写装饰器却很复杂.为了讲 ...

  6. SpringMVC的工作流程以及组件说明

    1. SpringMVC处理流程 2. SpringMVC架构 2.1 框架结构 2.2 框架流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherSer ...

  7. maven依赖大全

    1.oracle mysql驱动 <!-- mysql驱动支持 --> <dependency> <groupId>mysql</groupId> &l ...

  8. 笔记:Hibernate DML

    Hibernate 提供的HQL(Hibernate Query Language)语句也支持批量 update 和 delete 语法,语法格式如下: [UPDATE | DELETE] FROM ...

  9. Spring Boot Druid数据源配置

    package com.hgvip.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.su ...

  10. JSON Web Token - 在Web应用间安全地传递信息

    转载自:http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使 ...