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. python数据类型之pandas—DataFrame

    DataFrame定义: DataFrame是pandas的两个主要数据结构之一,另一个是Series —一个表格型的数据结构 —含有一组有序的列 —大致可看成共享同一个index的Series集合 ...

  2. N!

    求N! Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N ...

  3. d9

    # 整体进度# python基础 ——38天 2个月# 数据库 —— 存储数据和信息用的,本质上和文件没有区别 1-2周 # —— 增删改查更方便了# 前端 —— 2周# 框架 —— django 2 ...

  4. Winter-Camp欠债记录

    待完成: 球相交体积模板博客 Day3B题计算几何 Splay和Treap学习 [寒假]整理算法&模板

  5. 从 Firefox 35 版本开始,就无法兼容 PAC 式代理

    经过反复的测试,包括在“高级”选项里启用 PAC 代理的设置,也都无法使用 PAC 的代理——无法登陆 Twitter 账号,无法打开 Google 网页. 不知道各位有什么好办法吗? 以及中文火狐社 ...

  6. SSH 结构中 不同角色登录,显示不同的菜单

    关于这个功能,这里也就是提供一个思路,在做项目的时候因为要用到,肯定存在更好的方法,此思路仅供参考. 一.关于前台页面的接收方式 这里使用struts2的标签: <s:iterator valu ...

  7. python3读取excel数据

    import xlrd worksheet = xlrd.open_workbook('XXXX.xlsx')   #打开excel文件 sheet_names= worksheet.sheet_na ...

  8. 垃圾回收GC3种算法的衍生品 增量回收:预测和控制GC所产生的中断时间

    小结: 1.GC和程序处理的本质是无关的: 2.增量回收:预测和控制GC所产生的中断时间: 1. 分代回收 GC和程序处理的本质是无关的,因此它所消耗的时间越短越好.分代回收的目的,正是为了在程序 运 ...

  9. [archlinux] 迁移T7从T460s到T470

    这已经不是第一次做OS的迁移了,T7早已经迁移过多台设备了.所以,其实只需要如下三步: 1.  rsync 我一直有全系统备份的习惯,T7一直会不定期的全系统rsync到Tstation上面去.所以我 ...

  10. 封装一个axios请求后台的通用方法

    import axios from 'axios'; import constant from '@/js/const'; import alert from '@/js/alertView'; le ...