4ms java solution with 26 test cases passed.


  • 0
    J

    Following is my solutions with 26 test cases passed in 4ms. It's direct and easy to understand.

    public int thirdMax(int[] nums) {
    		int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE, thirdMax = Integer.MIN_VALUE;
    
    		boolean hasThirdMax = false;
    		for (int i = 0; i < nums.length; i++) {
    			if (nums[i] > max) {
    				max = nums[i];
    			}
    		}
    
    		for (int i = 0; i < nums.length; i++) {
    			if (nums[i] > secondMax && nums[i] < max) {
    				secondMax = nums[i];
    			}
    		}
    
    		for (int i = 0; i < nums.length; i++) {
    			if (nums[i] >= thirdMax && nums[i] < secondMax) {
    				thirdMax = nums[i];
    				hasThirdMax = true;
    			}
    		}
    		return hasThirdMax ? thirdMax : max;
    	}
    

    Waiting for your comments , guys.


Log in to reply
 

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