Third Maximum Number
![](https://shangan.gitbook.io/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-MQQ_aKuDz0ArQY9xrTi%252F-MQQ_hCHEF_NfTLb7c5f%252F-MQQhK6lAaBSDTrKohkL%252FWechatIMG22.png%3Falt%3Dmedia%26token%3D90bc890e-7a68-4f04-84cc-c23bbda959bd&width=768&dpr=4&quality=100&sign=bc39213f&sv=2)
/*
本算法答案由上岸科技提供。
上岸科技是一个专致力于高效培养北美留学生拥有实际面试能力的团体。
我们采用小班化线上,线下教学让学生更快,更好的学习到想要的知识。
团队主要由一群怀揣情怀于美国高校毕业的一线IT公司工程师构成。
我们坚信对于求职者算法并不是全部,合理的技巧加上适当的算法培训能够大大的提升求职成功概率也能大大减少刷题的痛苦。
正如我们的信仰:我们教的是如何上岸而不仅是算法。
更多信息请关注官网:https://www.shanganonline.com/
*/
class Solution {
public int thirdMax(int[] nums) {
if(nums.length == 1) return nums[0];
if(nums.length == 2) return Math.max(nums[0], nums[1]);
Integer max = null;
Integer second = null;
Integer third = null;
for(int i = 0; i < nums.length; i ++){
if(max == null ||nums[i] > max){
third = second;
second = max;
max = nums[i];
continue;
}else if((second == null || nums[i] > second) && nums[i] != max){
third = second;
second = nums[i];
continue;
}else if((third == null || nums[i] > third) && nums[i] != max && nums[i] != second){
third = nums[i];
}
}
return third == null ? max : third;
}
}
![](https://shangan.gitbook.io/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-MQQ_aKuDz0ArQY9xrTi%252F-MQQ_hCHEF_NfTLb7c5f%252F-MQQhNaEDp_XdzFm0Ypp%252FWechatIMG23.png%3Falt%3Dmedia%26token%3D0eec3fd8-70e7-4ac4-93e9-6cec19015bdb&width=768&dpr=4&quality=100&sign=f2223172&sv=2)
Last updated
Was this helpful?