Simple JAVA solution that beats 70%


  • 0
    G
    public class Solution {
        public int thirdMax(int[] nums) {
             
             Arrays.sort(nums);
             int j=0;
             int maxnum =0;
             for (int i=nums.length-1; i>=1; i--) {
                 
                 maxnum = Math.max(maxnum,nums[i]);
                 
                 if(nums[i-1] != nums[i]) {
                    j++; 
                 }
                 
                 if (j==2) return nums[i-1];
             }
             
             return maxnum;
        }
    }
    

Log in to reply
 

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