My solution beats 88.60 % of solutions.


  • 0
    P

    public class Solution {
    public int thirdMax(int[] nums) {
    long max1 = Long.MIN_VALUE;
    long max2 = Long.MIN_VALUE;
    long max3 = Long.MIN_VALUE;

        for(int i = 0; i < nums.length; i++) {
            if(nums[i] > max1) {
                max3 = max2;
                max2 = max1;
                max1 = nums[i];
            }
            
            else if(nums[i] < max1) {
                if(nums[i] > max2 && nums[i] != max1) {
                    max3 = max2;
                    max2 = nums[i];
                }
                if(nums[i] > max3 && nums[i] != max1 && nums[i] != max2)
                    max3 = nums[i];
            }
        }
        if(max3 != Long.MIN_VALUE)
            return (int) max3;
        else
            return (int) max1;
    }
    

    }


Log in to reply
 

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