My one pass C++ solution, easy to understand


  • 0
    Y

    My idea is to memorize the smallest middle number(a number which has a smaller number before it), so in the for loop, when current number is bigger than the smallest middle number before it, we find an increasing triplet.

     bool increasingTriplet(vector<int>& nums) {
            if (nums.size() < 3)return false;
        	int min = nums[0];
        	int middle = INT_MIN;
        	for (int i = 1; i < nums.size(); i++)
        	{
        		if (nums[i] <= min)min = nums[i];
        		else
        		{
        			if (middle == INT_MIN || middle >= nums[i])middle = 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.