C++ no long_min, no set, no pq solution


  • 0
     int thirdMax(vector<int>& nums) {
            vector<int>max(3,-INT_MAX-1);
            bool changed=false;
            for(int i=0;i<3;i++){
                for(int j=0;j<nums.size();j++){
                    if(i==0){ if(nums[j]>max[0]) max[i]=nums[j];}
                    else if(i==1){if(nums[j]>max[i]&&nums[j]!=max[0]) max[i]=nums[j];}
                    else if(nums[j]>=max[i]&&nums[j]!=max[1]&&nums[j]!=max[0]) max[i]=nums[j],changed=true;
                }
            }
            if((max[2]==-INT_MAX-1||nums.size()<3)&&changed==false) return max[0];
            return max[2];
        }
    

Log in to reply
 

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