python O(n) solution


  • 0
    F
    class Solution(object):
        def find132pattern(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            length=len(nums)
            x=[]
            a,b=float('inf'),-float('inf')
            flag=True
            for i in range(length):
                if nums[i]<=a and flag:
                    a=nums[i]
                    b=nums[i]
                elif nums[i]>=b:
                    b=nums[i]
                    flag=False
                elif nums[i]<b and nums[i]>a:
                    return True
                elif nums[i]<b and nums[i]<a:
                    if x==[]:
                        x.append([a,b])
                    else:
                        x.pop()
                        x.append([a,b])
                    a, b = nums[i],nums[i]
                    flag=True
                if x!=[]:
                    if nums[i]>x[0][0] and nums[i]<x[0][1]:
                        return True
            return False
    print Solution().find132pattern([3,5,0,3,4])
    

Log in to reply
 

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