414. Third Maximum Number

class Solution {
public:
    int thirdMax(vector<int>& nums) {
        long max1 = LONG_MIN, max2 = LONG_MIN, max3 = LONG_MIN;
        for (int num : nums) {
            if (max1 == num || max2 == num || max3 == num) continue;
            if (max1 == LONG_MIN || num > max1) {   //you can't use NULL as in JAVA
                max3 = max2;
                max2 = max1;
                max1 = num;
            } else
            if (max2 == LONG_MIN || num > max2) {
                max3 = max2;
                max2 = num;
            } else
            if (max3 == LONG_MIN || num > max3) {
                max3 = num;
            }
        }
        return max3 != LONG_MIN ? max3 : max1;
    }
};

results matching ""

    No results matching ""