LeetCode刷题(Third Maximum Number )
来源:互联网 发布:windows安装redis步骤 编辑:程序博客网 时间:2024/05/29 14:35
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.
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.
很自然想到使用TreeSet进行数据存储。
public class Solution { public int thirdMax(int[] nums) { TreeSet ts = new TreeSet(); for(int x=0;x<nums.length;x++) ts.add(nums[x]); if(ts.size()<=2) return (Integer)ts.last(); else { int y = (Integer)ts.pollLast(); int z = (Integer)ts.pollLast(); int v = (Integer)ts.pollLast(); return v; } }}
Submission Result: Accepted
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-Third Maximum Number
- LeetCode刷题【Array】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
- C++调用ArcEngine(C#)动态链接库
- XGBoost:大杀器xgboost指南
- 随机化快排
- linux export命令参数及用法详解--linux设置环境变量命令
- 考研---数学全书(复合函数的连续性和极限存在性)
- LeetCode刷题(Third Maximum Number )
- 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 第一行代码学习3(11)
- 探讨LoadRunner的并发用户和集合点
- Fastjson 序列化,反序列化Map对象排序问题(字符串转map,map转字符串)
- BFS(1)-胜利大逃亡
- Java多线程学习零散笔记
- Django-类视图
- PowerDesigner使用方法