C# Accepted solution


  • 0
    A
    public int ThirdMax(int[] nums) {
            if (nums == null) return -1;
                Array.Sort(nums);
    
                var decrementCount = 2;
                for (int i = nums.Length - 1; i > 0; i--)
                {
                    if (nums[i] != nums[i - 1])
                    {
                        decrementCount --;
                        if (decrementCount == 0)
                        {
                            return nums[i-1];
                        }
                    }
                }
                if (decrementCount != 0) return nums[nums.Length - 1]; 
                return -1;
        }
    

Log in to reply
 

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