The solution has accepted in C++, naive, easy to understand, self-explain


  • 0
    L
    class Solution {
    public:
        int thirdMax(vector<int>& nums) {
            int max,mid,min;
            int count = 0;
            int bottom = 0;
            max=mid=min=0x80000000;
            for(int i = 0; i< nums.size();i++){
                if(nums[i] == 0x80000000) bottom = 1; 
                if(nums[i] == max || nums[i] == mid || nums[i] == min) continue;
                if(nums[i] > max){
                    min = mid;
                    mid = max;
                    max = nums[i];
                    count ++;
                }else if(nums[i] > mid) {
                        min = mid;
                        mid = nums[i];
                        count ++;
                }else if(nums[i] > min){
                    min = nums[i];
                    count++;
                }
            }
           if(count + bottom >=3) return min;
    
           else return max; 
        }
    };
    

Log in to reply
 

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