Python O(n) time and O(1) space Solution with two variables


  • 0
    Q
    class Solution(object):
        def increasingTriplet(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            if not nums or len(nums) <= 2:
                return False
            
            exist_Tri = False
            
            first = nums[0]
            second = None
            for i in xrange(1, len(nums)):
                if nums[i] > first:
                    if second != None and nums[i] > second:
                        exist_Tri = True
                        break
                    elif second != None and nums[i] < second:
                        second = nums[i]
                    elif second == None:
                        second = nums[i]
                    else:
                        continue
                elif nums[i] < first:
                    first = nums[i]
                else:
                    continue
            return exist_Tri

Log in to reply
 

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