首页 > 代码库 > LeetCode "Third Maximum Number"

LeetCode "Third Maximum Number"

Straight-forward strategy.. please take care of all details - data type, assignment order etc.

class Solution {public:    int thirdMax(vector<int>& nums) {        long long v1, v2, v3;        v1 = v2 = v3 = std::numeric_limits<long long>::min();        size_t n = nums.size();                size_t cnt = 0;        for(int i = 0; i < n; i ++)        {            int v = nums[i];            if(v == v1 || v == v2 || v == v3) continue;            cnt ++;            if(v > v1)            {                v3 = v2;                v2 = v1;                v1 = v;            }            else if(v > v2)            {                v3 = v2;                v2 = v;            }            else if(v > v3)            {                v3 = v;               }        }                return cnt < 3 ? v1 : v3;    }};

LeetCode "Third Maximum Number"