31. 栈的压入、弹出序列

class Solution:
    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:

        stack = []
        while popped:
            if pushed and pushed[0] == popped[0]:
                pushed.pop(0)
                popped.pop(0)
            elif stack and stack[-1] == popped[0]:
                stack.pop()
                popped.pop(0)
            elif pushed:
                stack.append(pushed.pop(0))
            else:
                return False
        return True



上一篇:C语言极速学习开发——51单片机入门编程之使用KeilC51进行代码编译(点亮你心中学习的精神之灯-上)


下一篇:20191218 实验二《Python程序设计》实验报告