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. 一个I/O线程可以并发处理N个客户端连接和读写操作 I/O复用模型 基于Buf操作NIO可以读取任意位置的数据 Channel中读取数据到Buffer中或将数据 Buffer 中写入到 Channel 事件驱动消息通知观察者模式

    Tomcat那些事儿 https://mp.weixin.qq.com/s?__biz=MzI3MTEwODc5Ng==&mid=2650860016&idx=2&sn=549 ...

  2. ctf百度杯十二月场what_the_fuck(一口盐汽水提供的答案)

    目录 漏洞利用原理 具体利用步骤 漏洞利用原理 read(, &s, 0x20uLL); if ( strstr(&s, "%p") || strstr(& ...

  3. 【python】-- RabbitMQ 队列消息持久化、消息公平分发

    RabbitMQ 队列消息持久化 假如消息队列test里面还有消息等待消费者(consumers)去接收,但是这个时候服务器端宕机了,这个时候消息是否还在? 1.队列消息非持久化 服务端(produc ...

  4. Python进阶知识

    装饰器 迭代器 生成器 mixins 元编程 描述符 量化领域常用 列表推导式 字典推导式 高阶函数 lambda函数 三目表达式

  5. Java利用BufferedWriter写文本文件

    在本地写入保存的操作, 很多时候我们习惯使用Outputstream, 而其实写文本文件的时候, Java提供一个很好的工具给我们 ----- writer. 由于它是针对文本类型的文件操作, 所以如 ...

  6. nodejs与c语言交互应用实例

    nodejs与c/c++交互目前主流的方式有两种,node addon c++ 和 node-ffi . 1.node addon c++ 1)nodejs从c语言读取数据 addon.c #incl ...

  7. Jmeter(七)Mongodb的增删改查

    1.启动JMeter,新建线程组,设置线程组属性 2.右键添加-MongoDB Source Config 设置属性Server Address List:192.168.0.99 MongoDB S ...

  8. 数据库连接池libzdb

    官网:http://www.tildeslash.com/libzdb/ A small, easy to use Open Source Database Connection Pool Libra ...

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

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

  10. Problem I. Increasing or Decreasing MIPT-2016 Pre-Finals Workshop, Taiwan NTU Contest, Sunday, March 27, 2016

    题面: Problem I. Increasing or DecreasingInput file: standard inputOutput file: standard outputTime li ...