Java Sol in O(n) runtime and O(1) space


  • 0
    C
        public int thirdMax(int[] nums) {
            long num1 = Long.MIN_VALUE, num2 = num1, num3 = num2;
            
            for(int n : nums){
                if(n > num1){
                    num3 = num2;
                    num2 = num1;
                    num1 = n;
                }
                else if(n > num2 && n != num1){
                    num3 = num2;
                    num2 = n;
                }
                else if(n > num3 && n != num1 && n!=num2){
                    num3 = n;
                }
            }
            
            return (int) (num3 == Long.MIN_VALUE ? num1 : num3);
        }
    

Log in to reply
 

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