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.

思路就是依次判断, ans 分别是最大的, 第二大的, 第三大的, init 为None, 然后如果num在ans里面, 就continue, 否则的话依次判断大于最大的, 第二大的, 第三大的.

Note: 判断是否为None的时候, 如果是int有可能为0, 所以最好用 is None/ is not None 来判断!!!

Code

 class Solution:
def thirdMax(self, nums):
ans = [None]*3
for num in nums:
if num in ans:continue
if ans[0] is None or num > ans[0]:
ans[0], ans[1], ans[2] = num, ans[0], ans[1]
elif ans[1] is None or num > ans[1]:
ans[1], ans[2] = num, ans[1]
elif ans[2] is None or num > ans[2]:
ans[2] = num
return ans[2] if ans[2] is not None else ans[0]

[LeetCode] 414. Third Maximum Number_Easy的更多相关文章

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

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

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

  3. LeetCode 414 Third Maximum Number

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

  4. 【leetcode】414. Third Maximum Number

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

  5. LeetCode 414. 第三大的数(Third Maximum Number) 3

    414. 第三大的数 414. Third Maximum Number 题目描述 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是 O(n). 每 ...

  6. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  7. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  8. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  9. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

随机推荐

  1. String对象的比较

    public class StringTest { /* * equals 和 ==的区别 * 如果类中没有重写equals(),那么默认比较也是内存地址 * ==在基本数据类型中比较的是值! * i ...

  2. OOM

    在mnist上测试卷积网络时,显存不够导致下面的报错: ResourceExhaustedError (see above for traceback): OOM when allocating te ...

  3. 二叉苹果树|codevs5565|luoguP2015|树形DP|Elena

    二叉苹果树 题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的 ...

  4. .NET Core开发日志——Model Binding

    ASP.NET Core MVC中所提供的Model Binding功能简单但实用,其主要目的是将请求中包含的数据映射到action的方法参数中.这样就避免了开发者像在Web Forms时代那样需要从 ...

  5. JSCS: Please specify path to 'JSCS' package

    JSCS: Please specify path to 'JSCS' package 1.webStrom提示: JSCS: Please specify path to 'JSCS' packag ...

  6. ModelDriven & Preparable 接口

    一. Preparable接口 1.有prepare()方法,实现该接口的action在执行时,首先执行该方法,用于进行一些预处理.2.创建prepareXXX方法,则在执行XXX方法前,将先执行pr ...

  7. ios开发之 NSObject详解

    NSObject是大部分Objective-C类继承体系的根类.这个类遵循NSObject协议,提供了一些通用的方法,对象通过继承NSObject,可以从其中继承访问运行时的接口,并让对象具备Obje ...

  8. [yum] yum使用光盘安装或更新软件

    https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_ ...

  9. MyBati__mapper 中取值(#{} 或${}) 以及 parameterType为(基本类型 或复杂类型)

    参考资料: MyBatis学习笔记(三)——parameterType为基本类型时的使用方法 MyBatis的传入参数parameterType类型 1. MyBatis的传入参数parameterT ...

  10. swift中 ?和 !的区别

      可选类型(?)与强制解析运算符(!) ?是一种判断后再拆包的语法糖 !是一种强制拆包的语法糖   当你不确定有值的时候就可以用  ? 当你确定有值的时候可以用  !     ?的几种使用场景:1. ...