LeetCode : Third Maximum Number
来源:互联网 发布:基本编程 编辑:程序博客网 时间:2024/05/21 19:30
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 {public: int thirdMax(vector<int>& nums) { set<int> s; for (int num : nums) { s.insert(num); if (s.size() > 3) { s.erase(s.begin()); } } return s.size() == 3 ? *s.begin() : *s.rbegin(); }};
0 0
- LeetCode Third Maximum Number
- LeetCode-Third Maximum Number
- [Leetcode] Third Maximum Number
- LeetCode: Third Maximum Number
- leetcode---Third Maximum Number
- LeetCode : Third Maximum Number
- leetcode[Third Maximum Number]
- LeetCode Third Maximum Number
- leetCode-Third Maximum Number
- LeetCode #414: Third Maximum Number
- leetcode 414. Third Maximum Number
- LeetCode:414. Third Maximum Number
- LeetCode-414. Third Maximum Number
- Leetcode之Third Maximum Number
- LeetCode 414. Third Maximum Number
- [Leetcode]414. Third Maximum Number
- LeetCode 414. Third Maximum Number
- <leetcode>414. Third Maximum Number
- ofbiz中定时任务的一个重复执行的问题
- 序
- 在网站内建立地图(百度地图)
- Python基本语法
- Java中等待线程执行完毕
- LeetCode : Third Maximum Number
- Java Web开发7___通过数据库连接池连接MySQL 数据库
- window安装 Dubbo 注册中心(Zookeeper-3.4.6)
- 算法分析与设计第十一周:416. Partition Equal Subset Sum
- 如何解决复制来的js以及css等文件中文乱码问题
- linux套接字编程笔记(二)--简单的TCP套接字应用程序
- 安装系统(win7篇)新手专用
- 成为JavaGC专家(1)—深入浅出Java垃圾回收机制
- 读书笔记 —《JavaScript权威指南》