Java solution with wrapper Integer class.


  • 0
    Z
        Integer firstMax = null;
        Integer secondMax = null;
        Integer thirdMax = null;
        for (int i : nums) {
        	
    		if(firstMax == null || i > firstMax){
    			thirdMax = secondMax;
    			secondMax = firstMax;
    			firstMax = i;
    		}
    		else if( secondMax == null || i >secondMax){
    			if(i != firstMax){
    				thirdMax = secondMax;
    				secondMax=i;
    			}
    		}
    		else if(thirdMax ==null || i > thirdMax){
    				if(secondMax != i && firstMax!= i)
    					thirdMax=i;
    		}
    	}
        return thirdMax != null ? thirdMax : firstMax;

  • 0
    This post is deleted!

Log in to reply
 

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