leetcode_414. Third Maximum Number 返回数组中第三大的数,要求用O(n)的时间复杂度
来源:互联网 发布:迅捷网络的登录页面 编辑:程序博客网 时间:2024/05/21 09:21
题目:
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: 1Explanation: The third maximum is 1.
Example 2:
Input: [1, 2]Output: 2Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1]Output: 1Explanation: Note that the third maximum here means the third maximum distinct number.Both numbers with value 2 are both considered as second maximum.
题意:
给定一个非空整数数组,返回这个数组第三大的数,如果第三大的数不存在,则返回最大的数。要求用O(n)的时间复杂度。相同的数,表示一样大。
代码:
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
max_num = nums[0] #记录最大的数,初始化为nums[0]
count = 1 #记录最大的数的个数,用于寻找前三个最大的数
indx = 1 #从下标1开始遍历
while indx < n and count < 2 : #寻找第二大的数mid_num
if nums[indx] > max_num :
mid_num = max_num
max_num = nums[indx]
count += 1
else :
if nums[indx] < max_num :
mid_num = nums[indx]
count += 1
indx += 1
while indx < n and count < 3 : #继续寻找第三大的数min_num。当count=3时,表示前三个最大的数都找到,退出循环,开始遍历数组其他元素
if nums[indx] > max_num :
min_num = mid_num
mid_num = max_num
max_num = nums[indx]
count += 1
else :
if nums[indx] < max_num and nums[indx] > mid_num :
min_num = mid_num
mid_num = nums[indx]
count += 1
else :
if nums[indx] < mid_num :
min_num = nums[indx]
count += 1
indx += 1
for i in range(indx,n) : #开始遍历数组其他元素。之前在这个地方犯过错,让i从indx+1开始遍历,其实是不对的。因为上面while跳出循环时,如果是indx>=n,则此处也不用遍历了,所以一定是count=3条件才会执行此处代码,此时,i = indx 的元素还没比较,故应该从indx开始遍历
if nums[i] > max_num :
min_num = mid_num
mid_num = max_num
max_num = nums[i]
else :
if nums[i] < max_num and nums[i] > mid_num :
min_num = mid_num
mid_num = nums[i]
else :
if nums[i] < mid_num and nums[i] > min_num :
min_num = nums[i]
if count < 3 : #当count小于3,表示没有第三大的元素
return max_num
else :
return min_num
笔记:
这个题关键在初始化max_num,mid_num,min_num,代码用count变量(记录找到最大数的个数)来进行控制,依次定义mid_num和min_num。
表示效率还可以吧:
- leetcode_414. Third Maximum Number 返回数组中第三大的数,要求用O(n)的时间复杂度
- [LeetCode]414. Third Maximum Number第三大的数
- leetcode Third Maximum Number 第三大的数
- LeetCode_414 Third Maximum Number(easy)
- 返回数组中任意某个重复的数字--时间复杂度O(n)空间复杂度O(1)
- 从俩个有序数组中找出第K小的数。要求时间复杂度O(logmin(m,n))
- 找出大数组array中第k大的元素(要求时间复杂度O(1))
- Leetcode 414. Third Maximum Number 第三大的数 解题报告
- 有1,2,…,n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度O(1),一次只能交换两个数
- C++在已排序数组中查找和值确定的第一次出现的两个数(要求时间复杂度为o(n))
- (C#)实现时间复杂度为O(n)空间复杂度为O(1)的数组中奇偶数分离
- 如何在一个给定数组中找两个和为某个定值的数,要求时间复杂度为O(n)
- 在时间复杂度为 O(n) 内找出数组中出现次数超过一半的数
- 找出数组中出现次数超过一半的数(时间复杂度O(n))
- O(n)复杂度,求数组中第2大的数
- 在已排序的数组中寻找和是给定值的两项,要求时间复杂度为O(n)
- 找出一个数组中出现次数超过一半的那个数字,要求时间复杂度O(n)和空间复杂度为O(1)。
- 求所有子数组的和的最大值。要求时间复杂度为O(n)
- 设计模式--装饰者模式
- HDU 5971 icpc 大连 A Wrestling Match
- read .txt file
- c语言内存分配与释放
- 指向指针的指针
- leetcode_414. Third Maximum Number 返回数组中第三大的数,要求用O(n)的时间复杂度
- 剑指Offer(面试题43~45)
- 最小生成树+并查集+ kruskal
- java的instanceof关键字
- 分享python视频教程
- js实现发送短信验证码后的倒计时功能(无视页面刷新)
- 新增用户函数
- 5W2H分析法
- css3左右箭头