Java O(n) AC Solution !!!!!!!!!!!!


  • 0
    D

    public int thirdMax(int[] nums) {
    int a=Integer.MIN_VALUE, b=Integer.MIN_VALUE, c=Integer.MIN_VALUE;
    HashSet<Integer> set = new HashSet<>();
    for (int i = 0; i<nums.length; ++i) {
    set.add(nums[i]);
    if (nums[i] > a) {
    c = b;
    b = a;
    a = nums[i];
    } else if (nums[i] > b && nums[i] != a) {
    c = b;
    b = nums[i];
    } else if (nums[i] > c && nums[i] != b && nums[i] != a) {
    c = nums[i];
    }
    }
    if (set.size() >= 3) {
    return c;
    } else {
    return a;
    }
    }


Log in to reply
 

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