【LeetCode】414. Third Maximum Number 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/third-maximum-number/description/
题目描述
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.
题目大意
找出一个数组中第三大的数字,如果不存在的话,就返回最大数字。
解题方法
替换最大值数组
最基本的方法,找到最大值,然后每次把最大值移除,这样重复三次就得到了第三大的值。
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def setMax(nums):
_max = max(nums)
for i, num in enumerate(nums):
if num == _max:
nums[i] = float('-inf')
return _max
max1 = setMax(nums)
max2 = setMax(nums)
max3 = setMax(nums)
return max3 if max3 != float('-inf') else max(max1, max2)
使用set
用set去算,set的时间复杂度是O(n)。set的remove()方法可以去除某个值,不过每次只能去除一个。
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums_set = set(nums)
if len(nums_set) < 3:
return max(nums_set)
nums_set.remove(max(nums_set))
nums_set.remove(max(nums_set))
_max = max(nums_set)
return _max
这个方法的C++版本如下:
class Solution {
public:
int thirdMax(vector<int>& nums) {
set<int> s;
for (int num : nums) {
s.insert(num);
}
if (s.size() < 3) {
return maxset(s);
}
s.erase(maxset(s));
s.erase(maxset(s));
return maxset(s);
}
private:
int maxset(set<int> &s) {
int res = INT_MIN;
for (int c : s) {
res = max(res, c);
}
return res;
}
};
原来C++也有求最大值函数叫做max_element(),参数是起始和结束位置,返回的是指针。
class Solution {
public:
int thirdMax(vector<int>& nums) {
set<int> s;
for (int num : nums) {
s.insert(num);
}
if (s.size() < 3) return *max_element(s.begin(), s.end());
s.erase(*max_element(s.begin(), s.end()));
s.erase(*max_element(s.begin(), s.end()));
return *max_element(s.begin(), s.end());
}
};
三个变量
维护三个变量分别保存最大、次大、第三大的值,只需要遍历一次数组,找到这个数字和三个变量的大小关系,就能对应的更新对应的值。
为了去重,elif里面写了当前的Num要处于开区间内。
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# s1 > s2 > s3
s1, s2, s3 = float('-inf'), float('-inf'), float('-inf')
for num in nums:
if num > s1:
s1, s2, s3 = num, s1, s2
elif num < s1 and num > s2:
s2, s3 = num, s2
elif num < s2 and num > s3:
s3 = num
return s3 if s3 != float('-inf') else s1
这个方法的C++写法如下,为什么需要使用long long 呢?因为当第三大的数字是INT_MIN的话,你如果把三个数字都初始化成了INT_MIN就没法判断了。
class Solution {
public:
int thirdMax(vector<int>& nums) {
long long s1, s2, s3;
s1 = s2 = s3 = LLONG_MIN;
for (int num : nums) {
if (num > s1) {
s3 = s2;
s2 = s1;
s1 = num;
} else if (num < s1 && num > s2) {
s3 = s2;
s2 = num;
} else if (num < s2 && num > s3) {
s3 = num;
}
}
return s3 != LLONG_MIN ? s3 : s1;
}
};
日期
2018 年 2 月 4 日
2018 年 11 月 27 日 —— 最近的雾霾太可怕
【LeetCode】414. Third Maximum Number 解题报告(Python & C++)的更多相关文章
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- C#版 - Leetcode 414. Third Maximum Number题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
随机推荐
- Macbookpro快捷键
Alt键代替 Option 键,用 Windows 标志键代替 Command 键. 但是,使用的时候,Command健类似于Windows的Control键. 1.复制.粘贴.剪切.全选.查找 Co ...
- 汇编LED实验
汇编语言点亮LED 拿到一款全新的芯片,第一个要做的事情的就是驱动其 GPIO,控制其 GPIO 输出高低电平. GPIO口是IO口的一个功能之一. 一.接下来的步骤离不开芯片手册: 1.使能所有时钟 ...
- Spring整合Mybatis报 java.lang.ClassNotFoundException:org.springframework.core.metrics.ApplicationStartup,即:spring的版本过高,采用RELEASE稳定版
1.遇到的问题: 今天在弄spring整合mybatis的时候遇到一个小问题,如图所示: 简单来说:就是我的spring的xml文件没找到,我就奇了怪了,我所有的配置都没问题啊! 我pom.xml配置 ...
- Tomcat类加载机制和JAVA类加载机制的比较
图解Tomcat类加载机制 说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的 ...
- 基于MQTT协议实现远程控制的"智能"车
智能,但不完全智能 虽然我不觉得这玩意儿有啥智能的,但都这么叫就跟着叫喽. 时隔好几天才写的 其实在写这篇博文的时候我已经在做升级了,并且已经到了中后期阶段了. 主要是业余时间做着玩,看时间了. 规格 ...
- 8. LINUX shell 环境变量
wc –l file 计算文件行数, wc -w file 计算文件中的单词数, wc -c file 计算文件中的字符数 查看文件内容: cat .more
- Shell【常用知识总结】
一.常用知识总结 1.特殊变量($0,@,#,*,?) $0:当前脚本的文件名. $n:n是一个数字,表示第几个参数. $#:传递给脚本或函数的参数个数. $*:传递给脚本或函数的所有参数.当被双引号 ...
- APK 反编译以及遇到的问题
APK反编译: https://www.cnblogs.com/geeksongs/p/10864200.html 遇到的问题 https://www.jianshu.com/p/55bf5f688e ...
- GO 数字运算
大整数运算 // bigint project main.go package main import ( "fmt" "math" "math/bi ...
- JavaEE复习三
Http协议是基于请求/响应模式.无状态的协议:所有请求时相互独立的.无连续的:服务器无法记住与识别用户. 对于简单的页面浏览或信息获取,http协议可以完全胜任:对于需要提供客户端和服务器端交互的网 ...