题目:
思路+代码:
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
# 思路:使用一个辅助栈,按照pushed顺序入栈,如果栈顶元素==弹出序列的当前元素,则全部出栈;这里弹出序列只是充当一个查询作用;
test_stack = []
k = 0
for i in pushed:
test_stack.append(i)
while test_stack and test_stack[-1] == popped[k]:
test_stack.pop()
k += 1
return not test_stack