Java 6ms


  • 0
    Q
    public int thirdMax(int[] nums) {
        Arrays.sort(nums);
        int n=1;
        for(int i=nums.length-2;i>=0;i--){
            if(nums[i]!=nums[i+1]) n++;
            if(n==3) return nums[i];
        }
        return nums[nums.length-1];
    }

  • 0
    C

    but your solution is not O(n),since you sort your array,the time complexity should be O(nlogn)


Log in to reply
 

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