0ms C++ solution


  • 0
    C
    class Solution {
    public:
        int thirdMax(vector<int>& v) {
            long long *arr = new long long[3];
            fill_n(arr, 3, numeric_limits<long long>::min());
            for(auto i : v) {
                if(i == arr[0] || i == arr[1] || i == arr[2])
                    continue;
                if(i > arr[0]) {
                    arr[2] = arr[1];
                    arr[1] = arr[0];
                    arr[0] = i;
                }
                else if(i > arr[1]) {
                    arr[2] = arr[1];
                    arr[1] = i;
                }
                else if(i > arr[2])
                    arr[2] = i;
            }
            return arr[2] == numeric_limits<long long>::min() ? arr[0] : arr[2];
        }
    };
    

  • 0
    X

    I tried your answer and it fails on the test cases [1,1,2].


  • 0
    C

    @xiaojohnson
    the test cases were updated after I made this post. I updated my solution.


Log in to reply
 

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