# -*- coding:utf-8 -*-
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
stack=[]
index=0
while index<len(popV):
if popV[index] not in pushV and popV[index] not in stack:
return False
if popV[index] not in stack:
cur=pushV.index(popV[index])
for i in range(cur+1):
stack.append(pushV.pop(0))
stack.pop()
elif stack[-1]==popV[index]:
stack.pop()
else:
return False
index+=1
return True
我的算法之路45--栈的压入、弹出序列
最新推荐文章于 2025-07-01 16:19:09 发布
本文介绍了一种使用栈数据结构来验证给定的压栈和弹栈序列是否正确的算法实现。通过实例演示了如何判断一个序列是否能通过一系列的压栈和弹栈操作得到。
891

被折叠的 条评论
为什么被折叠?



