python leetcode 456. 132 Pattern

Related Topics中提示了用栈来做

132顺序 要做两个栈来做 一个相对大一个相对小

采用逆序231顺序  所以只需要判断当前值是否小于中间值即可 中间值又是从栈(底部最大顶部最小)中弹出来

class Solution:
    def find132pattern(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        mid=float('-inf')
        stack=[]
        for n in nums[::-1]:
            if n<mid: return True 
            while stack and stack[-1]<n:
                mid=stack.pop()
            stack.append(n)
        return False

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84750270