Leetcode——Third Maximum Number
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的更多相关文章
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- Leetcode: Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode "Third Maximum Number"
Straight-forward strategy.. please take care of all details - data type, assignment order etc. class ...
- 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 ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
随机推荐
- PacificA: Replication in Log-Based Distributed Storage Systems
PacificA: Replication in Log-Based Distributed Storage Systems - Microsoft Research https://www.micr ...
- Python3量化技术常用插件
1. 确定自己的系统为64位版本 2. 下载安装Python3 64位版本 如果要使用zipline,建议使用python3.5.另外发现很多东西要求的也是3.5. 主页地址: https://www ...
- 用Recover来实现更健壮的go程序
缘起:线上的go service 挂了,无法启动. 原因:采用的第三方库有个bug, 在go携程里面执行task的时候会产生out of range 的panic, 而我又把任务队列每次加载的时候重做 ...
- HTML容易遗忘内容(二)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...
- ASP.NET的优点
ASP.NET 是一个统一的 Web 开发平台,它提供开发人员创建企业级 Web 应用程序所需的服务.尽管 ASP.NET 的语法基本上与 ASP 兼容,但是它还提供了一个新的编程模型和基础结构以提高 ...
- Spring第三弹—–编码剖析Spring管理Bean的原理
先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- PAT 1077 Kuchiguse [一般]
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...
- Java中树的存储结构实现
一.树 树与线性表.栈.队列等线性结构不同,树是一种非线性结构. 一棵树只有一个根节点,如果一棵树有了多个根节点,那它已经不再是一棵树了,而是多棵树的集合,也被称为森林. 二.树的父节点表示法 树中除 ...
- visual studio NuGet
http://www.cnblogs.com/dudu/archive/2011/07/15/nuget.html 首先打开程序包管理器控制台:工具→Nuget程序包管理器→程序包管理器控制台 Ins ...
- 爬虫——请求库之selenium模块
阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解 ...