Java Solution


  • 0
    Z
    public static int thirdMax(int[] nums) {
    		int first=Integer.MIN_VALUE, second= Integer.MIN_VALUE, third = Integer.MIN_VALUE;
    		int count =0;boolean flag =false;
    		for(int i=0;i<nums.length;i++){
    			if(nums[i]>=first){
    				if(nums[i]==first) {
    					if(first==Integer.MIN_VALUE && !flag) {
    						count++;
    						flag=true;
    					}
    				}else{
    					count++;
    					third = second;
    					second=first;
    					first=nums[i];
    					flag =false;
    				}
    			}
    			else if(nums[i]>=second){
    				if(nums[i]==second){
    					if(second==Integer.MIN_VALUE){
    						count++;
    						flag=true;
    					}
    				}else{
    					count++;
    					third = second; 
    					second = nums[i];
    					flag= false;
    				}
    			}
    			else if(nums[i] >=third){
    				if(nums[i] ==third){
    					if(third==Integer.MIN_VALUE){
    						count++;
    						flag = true;
    					}
    				}
    				else{
    					count++;
    					third = nums[i];
    					flag = false;
    				}
    			}
    		}
    		if(count>2){
    			return third;
    		}else{
    			return first;
    		}
        }
    

Log in to reply
 

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