my 96.61% Solution at December forth


  • 0
    H

    If I check whether nums[i] appear in max1 or max2 or max3 first, then it will be only74.27%

    public class Solution {
        public int thirdMax(int[] nums) {
            if(nums.length == 0)return 0;
            int max1 =  nums[0];
            int max2 =  nums[0];
            int max3 =  nums[0];
            int count = 0;
            for(int i  = 1; i< nums.length ; i ++ ){
                if(nums[i]>max1){
                    max3 = max2 ;
                    max2 = max1;
                    max1 = nums[i];
    
                }
                else if((max2==max1||nums[i] > max2) && nums[i]!= max1){
                    max3 = max2>nums[i]?nums[i]:max2;
                    max2 = nums[i];
    
                }
                else if((max3==max2||nums[i]>max3 )&& nums[i]!= max2  && nums[i]!= max1){
                    max3 = nums[i];
    
                }
            }
            return max1!=max2&&max2!=max3? max3: max1;
            
            
        }
    }
    

Log in to reply
 

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