Leetcode 414. Third Maximum Number (Easy) (cpp)
来源:互联网 发布:初学数控车床编程教程 编辑:程序博客网 时间:2024/06/06 07:35
Leetcode 414. Third Maximum Number (Easy) (cpp)
Tag: Array
Difficulty: Easy
/*414. Third Maximum Number (Easy)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.*/class Solution {public: int thirdMax(vector<int>& nums) { set<int> s; for(int i = 0; i < nums.size(); ++i) { s.insert(nums[i]); if(s.size() > 3) { s.erase(s.begin()); } } if(s.size() < 3) { return *(--s.end()); } return *s.begin(); }};
0 0
- Leetcode 414. Third Maximum Number (Easy) (cpp)
- 414. Third Maximum Number Easy
- leetcode 414[easy]---Third Maximum Number
- leetcode 414. Third Maximum Number
- LeetCode:414. Third Maximum Number
- LeetCode-414. Third Maximum Number
- LeetCode 414. Third Maximum Number
- [Leetcode]414. Third Maximum Number
- LeetCode 414. Third Maximum Number
- <leetcode>414. Third Maximum Number
- Leetcode 414. Third Maximum Number
- 【LeetCode】 414. Third Maximum Number
- [leetcode]414. Third Maximum Number
- Leetcode-414. Third Maximum Number
- [leetcode]414. Third Maximum Number
- LeetCode 414. Third Maximum Number
- [LeetCode]414. Third Maximum Number
- Leetcode-414. Third Maximum Number
- repo下载国内链接android源码
- 树、二叉树概念
- VMware虚拟机中的CentOS安装Nginx后本机无法访问的解决办法
- java.lang.NoClassDefFoundError: org/slf4j/Logger 错误的解决
- 虚拟机当成一个纯粹人编译器(win-nfs-server)
- Leetcode 414. Third Maximum Number (Easy) (cpp)
- phpexcel 读取数据
- Leetcode 56. Merge Intervals (Hard) (cpp)
- 51Nod-1523-非回文
- Eclipse 配置 Tomcat 遇到的问题
- Leetcode 277. Find the Celebrity (Medium) (cpp)
- Discuz X3核心文件解析
- Apache Options Indexes FollowSymLinks详解
- C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。