Third Maximum Number


  • 0
    S

    '''
    class Solution
    {
    public int thirdMax(int[] nums)
    {
    Arrays.sort(nums);
    if(nums.length<3)
    return nums[nums.length-1];
    int max=1;
    int prev=nums[nums.length-1];
    for(int i=nums.length-2;i>=0;i--)
    {
    if(prev!=nums[i])
    {
    prev=nums[i];
    max++;
    }
    if(max==3)
    return nums[i];
    }
    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.