给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是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. readv 和 writev

    Unix 系统已经长时间支持名为 readv 和 writev 的 2 个系统调用. 这些 read 和 write 的"矢量"版本使用一个结构数组, 每个包含一个缓存的指针和一个 ...

  2. sql 根据列名查所属表名

    比如 有一个jueseID字段,想知道这个字段是哪个表里的. 第一步: select * from syscolumns where name = 'jueseID' 第二步: select * fr ...

  3. 【分块】P4135 作诗

    分块太暴力惹... 没做出来.看了题解qaq 分析: 两头$\sqrt{n}$暴力维护 预处理ans[i][j],sum[i][j] sum[i][j]是一个前缀和,前i块值为j的数量 ans[i][ ...

  4. git sync tags with remote

    git 同步遠程標籤 在 .git/config的 [remote "origin"] 下加了 fetch = +refs/tags/*:refs/tags/* 最後就變成 [re ...

  5. CF755F PolandBalls and Gifts

    题意:给你一个礼物的置换.有k个人忘带了礼物.一个人无法获得礼物为他自己没有带礼物或给他带礼物的那个人没有带礼物.求选择k个人,没有获得礼物的人数的最小值和最大值. n,k<=1e6. 标程: ...

  6. sort的cmp函数

    sort的cmp函数只能写return a>b;或者return a<b;

  7. LoadRunner中字符串的操作

    LoadRunner中字符串的操作 LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string); ...

  8. 转载:shell中#*,##*,#*,##*,% *,%% *的含义及用法

    介绍下Shell中的${}.##和%%使用范例,本文给出了不同情况下得到的结果.假设定义了一个变量为:代码如下:file=/dir1/dir2/dir3/my.file.txt可以用${ }分别替换得 ...

  9. python的meshgrid用法和3D库 mpl_toolkits.mplot3d 与PolynomialFeatures多项式库学习

    meshgrid import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Ax ...

  10. 点击回退时需要点击2次才可返回js

    为a加上window.location.href跳转页面时,再返回到此页面,再点击返回时需点击2次才能返回到前一个页面,原因竟然是href=“#”的原因,在html中#可做为锚点 http://blo ...