C++ solution, clear and clean


  • 0
    P
    class Solution {
    public:
        bool increasingTriplet(vector<int>& nums) { 
            // as long as there is one larger than min, we'll have two entries,
            // we can apply the same logic, as long as there is one larger than mid,
            // we'll have three. 
                                                                         
            int min =INT_MAX, mid = INT_MAX;
            for(int i = 0; i< nums.size(); ++i) { 
                if (nums[i] < min) { // minimum, first entry
                    min = nums[i];
                }
                if (nums[i] > min && nums[i] < mid){ // middle entry
                    mid = nums[i]; 
                }
                if (nums[i] > mid) { // last entry
                    return true; 
                }
            }
            return false;
        }
    };

Log in to reply
 

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