★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9783127.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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)。

示例 1:

输入: [3, 2, 1]输出: 1解释: 第三大的数是 1.

示例 2:

输入: [1, 2]输出: 2解释: 第三大的数不存在, 所以返回最大的数 2 .

示例 3:

输入: [2, 2, 3, 1]输出: 1解释: 注意,要求返回第三大的数,是指第三大且唯一出现的数。
存在两个值为2的数,它们都排第二。

12ms:用元组记录数值
 1 class Solution {
 2     func thirdMax(_ nums: [Int]) -> Int {
 3         var (num0,num1,num2) = (Int.min,Int.min,Int.min)
 4         for num in nums {
 5             if num == num0 || num == num1 || num == num2 {
 6                 continue
 7             }
 8             if num > num0 {
 9                 (num0,num1,num2) = (num,num0,num1)
10                 continue
11             }
12             if num > num1 {
13                 (num1,num2) = (num,num1)
14                 continue
15             }
16             if num > num2 {
17                 num2 = num
18             }
19         }
20         return num2 == Int.min ? num0 : num2
21     }
22 }


16ms

 1 class Solution {
 2     func thirdMax(_ nums: [Int]) -> Int {
 3         var firstMax = nums[0]
 4         var secondMax: Int?
 5         var thirdMax: Int?
 6
 7         // get max
 8         for num in nums {
 9             firstMax = max(firstMax, num)
10         }
11         // get 2nd max
12         for num in nums {
13             if num < firstMax {
14                 if let curSecondMax = secondMax {
15                     secondMax = max(curSecondMax, num)
16                 } else {
17                     secondMax = num
18                 }
19             }
20         }
21
22         if secondMax == nil {
23             return firstMax
24         }
25
26         for num in nums {
27             if num < secondMax! {
28                 if let curThirdMax = thirdMax {
29                     thirdMax = max(curThirdMax, num)
30                 } else {
31                     thirdMax = num
32                 }
33             }
34         }
35
36         return thirdMax ?? firstMax
37     }
38 }


16ms

 1 class Solution {
 2     func thirdMax(_ nums: [Int]) -> Int {
 3         var firstMax = Int.min
 4         var secondMax = Int.min
 5         var thirdMax = Int.min
 6         var count = 0
 7
 8         for num in nums {
 9             if num > firstMax {
10                 (thirdMax, secondMax, firstMax) = (secondMax, firstMax, num)
11                 count += 1
12             } else if num != firstMax && num > secondMax {
13                 (thirdMax, secondMax) = (secondMax, num)
14                 count += 1
15             } else if num != firstMax && num != secondMax && num > thirdMax {
16                 thirdMax = num
17                 count += 1
18             }
19         }
20
21         if count < 3 {
22             return firstMax
23         }
24
25         return thirdMax
26     }
27 }


20ms

 1 class Solution {
 2     func thirdMax(_ nums: [Int]) -> Int {
 3         var max = nums[0]
 4         var second: Int?
 5         var third: Int?
 6
 7         for i in 1..<nums.count {
 8             if nums[i] < max {
 9                 if second == nil {
10                     second = nums[i]
11                 } else {
12                     if second! < nums[i] {
13                         third = second!
14                         second = nums[i]
15                     } else if second! > nums[i] {
16                         if third == nil {
17                             third = nums[i]
18                         } else {
19                             if nums[i] > third! {
20                                 third = nums[i]
21                             }
22                         }
23
24                     }
25                 }
26
27             } else if nums[i] > max {
28                 third = second
29                 second = max
30                 max = nums[i]
31             }
32         }
33
34         if third != nil {
35             return third!
36         }
37
38         return max
39     }
40 }


24ms

 1 class Solution {
 2     func thirdMax(_ nums: [Int]) -> Int {
 3         var setNum = Set<Int>(nums)
 4         var queue = Queue<Int>(size: 3)
 5
 6         for num in setNum {
 7             if queue.count < 3 {
 8                 queue.push(value: num)
 9             } else if let min = queue.min() {
10                 if num > min {
11                     queue.pop()
12                     queue.push(value: num)
13                 }
14             } else {
15                 queue.push(value: num)
16             }
17         }
18
19         if queue.count < 3 {
20             while queue.count != 1 {
21                 queue.pop()
22             }
23         }
24
25         return queue.min() ?? 0
26     }
27 }
28
29 class Queue<T: Comparable> {
30     private var data = [T]()
31     private let size: Int
32     var count: Int {
33         return data.count
34     }
35     init(size: Int) {
36         self.size = size
37     }
38
39     func push(value: T) {
40         if data.isEmpty {
41             data.append(value)
42         } else if data.count + 1 > size {
43             if let last = data.first {
44                 if value < last {
45                     data.removeFirst()
46                     push(value: value)
47                 }
48             }
49         } else {
50             var toInsertAt = -1
51             for (index, dataValue) in data.enumerated() {
52                 if value > dataValue {
53                     toInsertAt = index
54                     break
55                 }
56             }
57             if toInsertAt == -1 {
58                 data.append(value)
59             } else {
60                 data.insert(value, at: toInsertAt)
61             }
62         }
63     }
64
65     @discardableResult
66     func pop() -> T? {
67         return data.popLast()
68     }
69
70     @discardableResult
71     func min() -> T? {
72         if data.isEmpty {
73             return nil
74         } else {
75             return data.last
76         }
77     }
78 }

转载于:https://www.cnblogs.com/strengthen/p/9783127.html

[Swift]LeetCode414. 第三大的数 | Third Maximum Number相关推荐

  1. leetcode414. 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  2. Nginx windows 版本 修改句柄数 解决 maximum number of descriptors supported by select() is 1024 while waiting

    这几天在项目上遇到个关于Nginx的问题,就是在测试中使用windows 的Nginx使用(主要用来做Socket的负载均衡),测试过程中没有发现问题(主要是测试的量太少,生产环境中差不多有1000个 ...

  3. C#LeetCode刷题之#414-第三大的数(Third Maximum Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...

  4. Oracle 数据库设置最大进程数参数方法,oracle最大进程数满了处理方法,sysdba管理员登录报“maximum number of processes (150) exceeded“问题解决

    oracle 数据库使用 sysdba 管理员登录报: ORA-00020: maximum number of processes (150) exceeded 译:超过了最大进程数(150) 方法 ...

  5. 【LeetCode】414. 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1]输出: 1解释: 第三大的数是 1. 示例 2: ...

  6. 【LeetCode】414.第三大的数

    题目描述 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 ...

  7. ajax 最大链接数_leetcode之第三大的数

    序 本文主要研究一下leetcode之第三大的数 题目 给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n).示例 1:输入: [3, 2, 1 ...

  8. LeetCode Algorithm 414. 第三大的数

    414. 第三大的数 Ideas emmmm,内置排序算法YYDS,三行代码解决. Code C++ class Solution {public:int thirdMax(vector<int ...

  9. leetcode|第三大的数java题解

    由于在面试作业帮.好未来侧开实习面试的时候都被问到了这道题,所以我就来写个题解吧 给你一个非空数组,返回此数组中 第三大的数 .如果不存在,则返回数组中最大的数. 示例 1: 输入:[3, 2, 1] ...

最新文章

  1. protobuf前后端解析_Go语言微服务架构实战:第七节 Protobuf协议语法及原理
  2. Ice笔记-利用Ice::Application类简化Ice应用
  3. java队列等待唤醒_Java深入学习29:线程等待和唤醒的两个方案
  4. mysql数据库txt备份linux_linux备份mysql数据库
  5. win7里面计算机叫什么,Win7电脑中的mrt.exe是甚么文件
  6. Spring MVC DispatcherServlet介绍
  7. 从源码角度详解Java的Callable接口
  8. 【人脸识别】arcface详解
  9. 预见2021:《2021年中国呼叫中心产业全景图谱》 未来十年八大预测
  10. 使用Slim框架创建一个JSON RESTfull API
  11. 微信头像测试软件,心理测试:四个微信头像选一个,测试你的真实性格?
  12. PMBOK(第六版) PMP笔记——《七》第七章(项目成本管理)
  13. java调用rapidminer_基于RapidMiner开发问题和解决
  14. 下载文件downFile
  15. c语言小写A对应数字,C语言ascII与数字转化的问题,值得新手看看
  16. 单亲妈妈开米粉店,赚得比当白领的女儿还多,做生意一定要懂方法
  17. Unity和Autodesk:通过更高效的工作流程提供沉浸式体验
  18. android zip文件读写,【Android】Zip文件解压方法
  19. 《一、企业级监控之使用docker容器化部署grafana》
  20. UITableviewCell 使用Masonry撑开cell高度 遇见[LayoutConstraints] Unable to simultaneously satisfy constraints

热门文章

  1. 修改csdn博客的名称
  2. 计算机毕业设计java+springboot+vue学生宿舍管理系统
  3. 个人基因组测序降到 500 元以内后基因组学将有哪些大的改变?
  4. Makefile新手?千万别错过了《驾驭Makefile》
  5. 第4章第11节:图表:使用柱形图表制作学生成绩表 [PowerPoint精美幻灯片实战教程]
  6. 2021年Java者未来的出路在哪里
  7. 联通鸿蒙卡怎么样,联通腾讯大王卡对比百度大神卡:到底选谁看完就明白了
  8. 兴业消费金融股份公司市场总监杜一谦:合规化进程中的消费金融探索
  9. html图片自动适应窗口大小,使用CSS自动调整浏览器大小的图片大小
  10. IDEA开发中包(package)的作用