给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是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. Python flask 构建微电影视频网站✍✍✍

    Python flask 构建微电影视频网站  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大 ...

  2. 论文阅读-(CVPR 2017) Kernel Pooling for Convolutional Neural Networks

    在这篇论文中,作者提出了一种更加通用的池化框架,以核函数的形式捕捉特征之间的高阶信息.同时也证明了使用无参数化的紧致清晰特征映射,以指定阶形式逼近核函数,例如高斯核函数.本文提出的核函数池化可以和CN ...

  3. Java核心-03 谈谈final、finally、 finalize有什么不同?

    今天,我要问你的是一个经典的 Java 基础题目,谈谈 final.finally. finalize 有什么不同? 典型回答 final 可以用来修饰类.方法.变量,分别有不同地意义,final修饰 ...

  4. windows10 vs2019 + opencv 3.4.7环境搭建

    windows vs2019 + opencv 3.4.7环境搭建 安装Opencv 3.4.7 下载 Opencv 第1步 进入 opencv releases 页面,点击 "Window ...

  5. Sightseeing Cows

    Sightseeing Cows 给出一张图,点数为L,边数P,并给出边的边权\(\{b_i\}\),再给处每个点的点权,求一条起点和终点相同的路径,并使其点权之和除以边权之和最大,注意,路径中点权只 ...

  6. Consul 安装的与启动

    1.下载地址:https://www.consul.io/downloads.html linux 下载地址: wget https://releases.hashicorp.com/consul/0 ...

  7. Ubuntu+Ruby+MySQL+Nginx+Redmine部署记录

    (2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2016年7月26日) 周五的时候老大布置了一个任务下来,要部署一个Redmine用于研发部,同时升级工作室的Redmine ...

  8. 牛客NOIP暑期七天营-提高组6

    目录 A-积木大赛 题目描述 link 题解 代码 B-破碎的序列 题目描述 link 题解 C-分班问题 题目描述 link 题解 比赛链接 官方题解 A-积木大赛 题目描述 link 题解 标签: ...

  9. windows API 第 18篇 FindFirstVolume FindNextVolume

    函数定义:Retrieves the name of a volume on a computer. FindFirstVolume is used to begin scanning the vol ...

  10. 01.Hibernate快速入门

    第一步:下载Hibernate5的运行环境 https://sourceforge.net/projects/hibernate/files/hibernate-orm/ 第二步:在数据库创建表 Cr ...