Question

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.

Solution 1

用两个标识符表示,第二大的数和第三大的数是否已经赋值。

class Solution {
public:
int thirdMax(vector<int>& nums) {
if (nums.size() == 1)
return nums[0];
if (nums.size() == 2)
return max(nums[0], nums[1]); long one = nums[0];
long two = LONG_MIN;
long three = LONG_MIN;
bool two_flag = true;
bool three_flag = true; for (int i = 1; i < nums.size(); i++) {
if (nums[i] > one) {
if (one > two) {
if (two > three) {
three = two;
three_flag = false;
}
two = one;
two_flag = false;
}
one = nums[i];
}
else if ((two_flag || nums[i] > two) && nums[i] != one) {
if (two > three) {
three = two;
three_flag = false;
}
two = nums[i];
two_flag = false;
}
else if ((three_flag || nums[i] > three) && nums[i] != one && nums[i] != two) {
three = nums[i];
three_flag = false;
} }
if (!three_flag)
return three;
else
return one;
}
};

Solution 2

用STL set,因为set中最多有3个元素,因此操作都是常量时间的,这样就可以保证总的时间是O(n),而且set会自动排序,底层实现是二叉排序树,因为有利于比较。

class Solution {
public:
int thirdMax(vector<int>& nums) {
set<int> s;
for (int i = 0; i < nums.size(); i++) {
if (s.size() < 3)
s.insert(nums[i]);
else {
set<int>::iterator it = s.begin();
if (s.find(nums[i]) == s.end() && (nums[i] > *(it) || nums[i] > *(++it) || nums[i] > *(++it)) ) {
s.erase(it);
s.insert(nums[i]);
}
}
}
if (s.size() == 3)
return *(s.begin());
else
return *(s.rbegin());
}
};

Leetcode——Third 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. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  3. Leetcode: Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  4. LeetCode "Third Maximum Number"

    Straight-forward strategy.. please take care of all details - data type, assignment order etc. class ...

  5. 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 ...

  6. C#版 - Leetcode 414. Third Maximum Number题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

  8. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  9. LeetCode 321. Create Maximum Number

    原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...

随机推荐

  1. (转)聊聊Servlet、Struts1、Struts2以及SpringMvc中的线程安全

    前言 很多初学者,甚至是工作1-3年的小伙伴们都可能弄不明白?servlet Struts1 Struts2 springmvc 哪些是单例,哪些是多例,哪些是线程安全? 在谈这个话题之前,我们先了解 ...

  2. ubuntu 打开 gbk编码的txt乱码

    iconv -f gbk -t utf8 filename.txt > filename.txt.utf8

  3. 聊聊高并发(三十四)Java内存模型那些事(二)理解CPU快速缓存的工作原理

    在上一篇聊聊高并发(三十三)从一致性(Consistency)的角度理解Java内存模型 我们说了Java内存模型是一个语言级别的内存模型抽象.它屏蔽了底层硬件实现内存一致性需求的差异,提供了对上层的 ...

  4. python插入记录后获取最后一条数据的id

    python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id()) 参考:https://blog.csdn.net/qq_37788558/article ...

  5. 网页中Cache各字段含义

    Pragma 当该字段值为"no-cache"的时候(事实上现在RFC中也仅标明该可选值),会知会客户端不要对该资源读缓存,即每次都得向服务器发一次请求才行. Expires 有了 ...

  6. Linux常见错误之Could not get lock /var/lib/dpkg/lock - open

    在Ubuntu系统上安装vim是遇到的问题: root@ubuntu:/# vim The program 'vim' can be found in the following packages: ...

  7. Font: a C++ class

    Font: a C++ class        This class is used in Fractal Generator.    Avi Examples The header fileFon ...

  8. 设计模式学习(二):面向对象设计原则与UML类图

    一.UML类图和面向对象设计原则简介 在学习设计模式之前,需要找我一些预备知识,主要包括UML类图和面向对象设计原则. UML类图可用于描述每一个设计模式的结构以及对模式实例进行说明,而模式结构又是设 ...

  9. win10怎样取消电脑自动锁屏

    笔记本经常用着用着就锁屏了 解决方法: 打开控制面板的电源选项,更改“使计算机进入睡眠状态”时间为“从不”.

  10. windows 下安装 rabbitmq报init terminating in do_boot错误

    好长时间没有写东西了,记一个安装笔记吧. 目前市面上比较常用的几个消息中间件,rabbitmq算是风评比较好的,所以就拿来安装一下玩玩喽(很有可能也仅限于是安装一下....)安装过程不表,无非是下载E ...