给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。

示例 1:

输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1.

示例 2:

输入: [1, 2] 输出: 2 解释: 第三大的数不存在, 所以返回最大的数 2 .

示例 3:

输入: [2, 2, 3, 1] 输出: 1 解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。 存在两个值为2的数,它们都排第二。

  1. class Solution {
  2. public:
  3. int thirdMax(vector<int>& nums) {
  4. int first = -2147483648;//INT_MIN
  5. int second = -2147483648;
  6. int third = -2147483648;
  7. int cnt = 0;
  8. map<int, int> visit;
  9. for(int i = 0; i < nums.size(); i++)
  10. {
  11. if(visit[nums[i]] != 0)
  12. continue;
  13. cnt++;
  14. if(nums[i] > first)
  15. {
  16. third = second;
  17. second = first;
  18. first = nums[i];
  19. }
  20. else if(nums[i] > second)
  21. {
  22. third = second;
  23. second = nums[i];
  24. }
  25. else if(nums[i] > third)
  26. {
  27. third = nums[i];
  28. }
  29. visit[nums[i]]++;
  30. }
  31. if(cnt > 2)
  32. return third;
  33. else
  34. return first;
  35. }
  36. };

Leetcode414Third Maximum Number第三大的数的更多相关文章

  1. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  2. 414 Third Maximum Number 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.示例 2:输入: ...

  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. [Swift]LeetCode414. 第三大的数 | Third Maximum Number

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

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

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

  6. C#LeetCode刷题之#414-第三大的数(Third Maximum Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...

  7. ORA-00018: maximum number of sessions exceeded 超出最大会话数

    ORA-00018: maximum number of sessions exceededORA-00018: 超出最大会话数 Cause:       All session state obje ...

  8. 414. Third Maximum Number数组中第三大的数字

    [抄题]: Given a non-empty array of integers, return the third maximum number in this array. If it does ...

  9. LeetCode 414 Third Maximum Number

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

随机推荐

  1. Hibernate的Hello World!

    一.创建Java工程,新建Lib文件夹,加入Hibernate和数据库(如MySql.Oracle.SqlServer等)的Jar包 二.创建 hibernate.cfg.xml 文件,并配置,配置项 ...

  2. 将本地数据库迁移到云端RDS数据库

  3. 2、docker镜像管理

    Docker镜像管理 镜像是Docker容器的基础,想运行一个Docker容器就需要有镜像.我们上面已经学会了使用search搜索镜像.那么这个镜像是怎么创建的呢? 创建镜像 镜像的创建有以下几种方法 ...

  4. html--浮动高度塌陷问题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. 自己编写jquery插件

    http://blog.csdn.net/chenxi1025/article/details/52222327 https://www.cnblogs.com/ajianbeyourself/p/5 ...

  6. Bitcoin 的基本原理

    昨天读到了 Bitcoin 的中文介绍,觉得非常有意思.不过上面这篇文章解释的非常不靠谱,我花了一晚上去Bitcoin的官方网站 仔细研究了一下,总算理解了其原理.感觉非常有启发,尤其是对虚拟货币的流 ...

  7. SPSS分析过程可自动化,你知道吗

    SPSS分析过程可自动化,你知道吗 在使用SPSS的过程中,有时候会遇到重复进行相同分析操作的情况,或者分析过程很复杂的情况. 这时候我们多么希望SPSS能够记住上一次的分析步骤,不要让我们重复的去点 ...

  8. 解决windows8.1的依赖

  9. HBase 物理视图

  10. 6.Spring【AOP】XML方式

    1.AOP术语 1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点 2. Pointcut(切入点):所谓切 ...