A method which is only 8ms


  • 0
    W
      public int thirdMax(int[] nums) {
            Arrays.sort(nums);
            if (nums.length < 3) {
                return nums[nums.length - 1];
            }
            int count = 1;
            int tmp = nums[nums.length - 1];
            for (int i = nums.length - 2; i >= 0 && count < 3; i--) {
                if (nums[i] < tmp) {
                    tmp = nums[i];
                    count++;
                }
            }
            if (count == 3) {
                return tmp;
            } else {
                return nums[nums.length - 1];
            }
        }
    

Log in to reply
 

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