leetcode 414. Third Maximum Number 第三大数据
来源:互联网 发布:floyd算法详细解释 编辑:程序博客网 时间:2024/06/05 07:29
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.
本提题意就是寻找第三大的数据,这里我们使用一个set来表示,注意set是一个高度平衡的二叉搜索树,所以注意set数据结构的使用
set–常见成员函数及基本用法
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <bitset>using namespace std;class Solution{public: int thirdMax(vector<int>& nums) { set<int> ss; for (int i : nums) { ss.insert(i); if (ss.size() > 3) ss.erase(ss.begin()); } if(ss.size()==3) return *(ss.begin()); else return *(ss.rbegin()); }};
阅读全文
0 0
- 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
- 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
- Ipv4转发实验
- android 阻断seekbar的触摸事件
- 360私有云平台Elasticsearch服务初探
- intellij idea设置代码提示不区分大小写
- Swarm 如何存储数据?- 每天5分钟玩转 Docker 容器技术(103)
- leetcode 414. Third Maximum Number 第三大数据
- 基于CBO的SQL优化和Oracle实例优化
- HTML5--CSS3制作的三级下拉菜单
- 流输入>>、流输出<<作为友元重载
- VSCode+Clang+OpenGL(GLUT)
- Java作业-猜数字(多线程)
- RecyclerView的样式
- 你真的会php吗?
- Spring(一):HelloWord