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. 【Python数据挖掘】回归模型与应用

    线性回归 ( Linear Regression ) 线性回归中,只包括一个自变量和一个因变量,且二者的关系可用一条直线近似表示,这种回归称为一元线性回归. 如果回归分析中包括两个或两个以上的自变量, ...

  2. ArcEngine之Provide your license server administrator with the following information.error code =-42

    今天打开VS,不一会就出现了下面的对话框,感到非常疑惑,仔细一想,原来是昨天不小心把权限弄错了! 解决办法:在控价中找到AxLicenseControl,右键属性,把权限改为ArcGIS Engine ...

  3. C#日期处理(转) 太忘记了,备忘

    //今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-1).ToShortDateStrin ...

  4. Vue1.x 到Vue2.0的一个变化

    小弟初来乍到,写的不好的地方还望指正.谢谢各位! 废话不多说  进入正题: Vue1.x到2.0的一个变化 1. 在每个组件模板,不在支持片段代码  组件中模板: 之前:     <templa ...

  5. maven之jre默认配置

    需要在用户或者全局settings.xml中做如下配置 例:用户配置: 添加代码: <profile> <id>jdk-1.8</id> <activatio ...

  6. gui设计

    芯艺,你好! ... ... ... 芯艺,再见! http://www.chipart.cn/ 附件 http://files.cnblogs.com/files/dong1/%E8%89%BA%E ...

  7. jQuery异步加载数据并添加事件示例

    当时项目是通过树形栏进行权限控制的,管理员可以对从数据库去的数据动态生成树形栏进行增删改查操作,可是用$(".XX").click();方法是不行的. 1.之前用的是jq1.4.3 ...

  8. facebook工具xhprof的安装与使用-分析php执行性能(转载)

    下载源码包的网址 http://pecl.php.net/package/xhprof

  9. repo使用

    repo常用指令: 1.repo init(下载repo并克隆manifest) repo init -u URL [OPTIONS] Options: -u:制定一个URL,其连接到一个manife ...

  10. HDU3074: Multiply game(线段树单点更新,区间查询)

    题目: 传送门 题解:线段树模板题目. 对递归的题目始终理解不好,我的痛啊,在水的题目都要写很长时间. #include <iostream> #include <string.h& ...