Offer 31. 栈的压入、弹出序列
1. python
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
stack ,i = [],0
for num in pushed:
stack.append(num)
while stack and stack[-1]==popped[i]: #此处是while 循环 ,必须家stack 非空 不然outoffange
stack.pop()
i+=1
return not stack
#1、首先pushed 入栈第一个元素, 如果 poped 【i】== stack 栈顶元素, 出栈 and i++