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. 淘宝Tengine 2.1.2 稳定版(nginx/1.6.2) Centos 6.5安装教程

    淘宝Tengine 2.1.2 稳定版(nginx/1.6.2) Centos 6.5 安装教程 Tengine 简介: Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大 ...

  2. Flask web开发之路九

    flask_scripts介绍 项目结构如下: flask_script_demo.py文件: from flask import Flask app = Flask(__name__) @app.r ...

  3. python爬虫+使用cookie登录豆瓣

    2017-10-09 19:06:22 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言: 先获得cookie,然后自动登录豆瓣和新浪微博 系统环境: 64位win10系统,同时装pytho ...

  4. 洛谷P1182 数列分段【二分】【贪心】

    题目:https://www.luogu.org/problemnew/show/P1182 题意: 有n个数,要分成连续的m段.将每段中的数相加,问之和的最大值的最小值是多少. 思路: 和P1316 ...

  5. Page13:跟踪问题、最优控制[Linear System Theory]

    内容包含跟踪问题及其结构框图.内模原理.可跟踪条件 各种线性二次型最优控制问题指标含义

  6. [cloud][OVS][sdn] Open vSwitch 初步了解

    What is Open vSwitch? Open vSwitch is a production quality, multilayer virtual switch licensed under ...

  7. [daily][editer] 二进制编辑工具 hyx

    用了众多之后,终于发现了一个好用的二进制编辑工具: hyx https://yx7.cc/code/ https://en.wikipedia.org/wiki/Comparison_of_hex_e ...

  8. 如何通过钉钉扫码登录odoo

    更加方便快捷的登录odoo,实现免密码登录,有需要此模块朋友加我微信18310744639 1.首先你需要一个钉钉管理员权限,以便获取appid, appsecret,corpid, corpsecr ...

  9. day5_集合

    集合也是一种数据类型,一个类似列表东西,它的特点是无序的,不重复的,也就是说集合中是没有重复的数据 集合的作用: 1.它可以把一个列表中重复的数据去掉,而不需要你再写判断---天生去重 2.可以做关系 ...

  10. SYSAUX表空间如何清理

    ############################################################ 操作方案################################### ...