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

示例 1:

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

示例 2:

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

示例 3:

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

class Solution {
public:
int thirdMax(vector<int>& nums) {
int first = -2147483648;//INT_MIN
int second = -2147483648;
int third = -2147483648;
int cnt = 0;
map<int, int> visit;
for(int i = 0; i < nums.size(); i++)
{
if(visit[nums[i]] != 0)
continue;
cnt++;
if(nums[i] > first)
{
third = second;
second = first;
first = nums[i];
}
else if(nums[i] > second)
{
third = second;
second = nums[i];
}
else if(nums[i] > third)
{
third = nums[i];
}
visit[nums[i]]++;
}
if(cnt > 2)
return third;
else
return first;
}
};

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. P1082 同余方程(扩欧模板)

    https://www.luogu.org/problem/P1082 #include <iostream> #include <cstdio> #include <q ...

  2. Python遇到的第一个问题

    1.运行如下代码: 输入成绩80之后报错: 2.问题分析:字符串跟整型不能比 3.查看score的类型 print(type(score)), 由此看出score是string类型的,因为input接 ...

  3. 位运算 - 左移右移运算符 >>, <<, >>>

    1-左移运算符m<<n,表示把m左移n位.左移n位的时候,最左边的n位数将被丢弃,同时在最右边补上n个0.例如: 00001010<<2 = 00101000 10001010 ...

  4. Java参数校验工具validation实践

    介绍 在项目开发当中,数据校验是你必须要考虑和面对的事情,为此要写上一大串的代码进行校验,这样就会导致代码冗余和一些管理的问题. 例如下面的代码: public void push(List<L ...

  5. Apache Flink 进阶(一):Runtime 核心机制剖析

    1. 综述 本文主要介绍 Flink Runtime 的作业执行的核心机制.首先介绍 Flink Runtime 的整体架构以及 Job 的基本执行流程,然后介绍在这个过程,Flink 是怎么进行资源 ...

  6. cache方法用于查询缓存操作,也是连贯操作方法之一。

    cache方法用于查询缓存操作,也是连贯操作方法之一. cache可以用于select.find和getField方法,以及其衍生方法,使用cache方法后,在缓存有效期之内不会再次进行数据库查询操作 ...

  7. Git命令汇总(转)

    转自:http://blog.csdn.net/esrichinacd/article/details/17645951 图片看不清请点击放大

  8. 04.如何升级扩展以支持Visual Studio 2019

    更新.vsixmanifest 我们需要对.vsixmanifest文件进行一些更新.首先,我们必须更新支持的VS版本范围 <InstallationTarget> 这是一个版本,支持Vi ...

  9. python 中的 is 方法 总结

    isidentifier: 判断变量名是否合法 iskeyword:是否为内置关键字

  10. 让html里的js脚本延迟5秒运行

    setTimeout( function(){ //add your code}, 5 * 1000 );//延迟5000毫米