Share my C++ solution with comment,easy to understand


  • 0
    V
    class Solution {
    public:
        bool increasingTriplet(vector<int>& nums) {
            int n = nums.size();
            if (n < 3)
                return false;
                
            //maintain two number first ,second and make them meet the condition:first < second
            //if we can find another number nums[i] and nums[i] > second,then return true 
            int first = INT_MAX;
            int second = INT_MAX;
            
            for (int i = 0; i < n; ++i)
            {
                if (first >= nums[i])
                    first = nums[i];
                else if (second >= nums[i])
                    second = nums[i];
                else
                    return true;
            }
            
            return false;
        }
    };

Log in to reply
 

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