Short Clear C++ solution, no set or pq.

  • 14
    int thirdMax(vector<int>& nums) {
        long long a, b, c;
        a = b = c = LLONG_MIN;
        for (auto num : nums) {
            if (num <= c || num == b || num == a) continue;
            c = num;
            if (c > b) swap(b, c);
            if (b > a) swap(a, b);
        return c == LLONG_MIN ? a : c;

  • 0

    Good work! Pretty clear!

  • 1

    hi man, I think it will not pass this test case: [1,2,LLONG_MIN],

    because that case c=LLONG_MIN (from the nums), but you code will return a

  • 1

    @shawn49 Yes. But man, the case does not exist because the input nums is a vector of int. It can't be LLONG_MIN, which is a long long type.

  • 0


    you are totally right man.

    I just understand that is why you used long long type

    Thank you

  • 3

    But this may create problems if a test-case including LLONG_MIN is included changing the datatype of the vector to long long

Log in to reply

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