My one pass C++ solution, easy to understand

  • 0

    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];
        			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.