C# short and concise solution


  • 0
    P
        public int ThirdMax(int[] nums) {
            if (nums == null || nums.Length == 0) return int.MinValue;
            
            long max1 = long.MinValue, max2 = max1, max3 = max1;
    
            foreach(int num in nums) {
                if (num == max1 || num == max2 || num == max3) continue;
         
                max3 = Math.Max(max3, Math.Min(max2,num));
                max2 = Math.Max(max2, Math.Min(max1,num));
                max1 = Math.Max(max1,num);
            }
            return max3 == long.MinValue ? (int)max1 : (int)max3;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.