https://leetcode-cn.com/problems/third-maximum-number/
class Solution { public int thirdMax(int[] nums) { TreeSet<Integer> s = new TreeSet<Integer>(); for (int num : nums) { s.add(num); if (s.size() > 3) { s.remove(s.first()); } } return s.size() == 3 ? s.first() : s.last(); } }
class Solution { public int thirdMax(int[] nums) { long a=Long.MIN_VALUE; long b=Long.MIN_VALUE; long c=Long.MIN_VALUE; for(int num:nums){ if(num>a){ c=b; b=a; a=num; } else if(a>num&&num>b){ c=b; b=num; } else if(b>num&&num>c){ c=num; } } return c==Long.MIN_VALUE?(int)a:(int)c; } }