Java solution by sorting


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

Log in to reply
 

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