0
点赞
收藏
分享

微信扫一扫

剑指31 栈的压入、弹出序列

乌龙茶3297 2022-05-23 阅读 78


剑指31 栈的压入、弹出序列_出栈

# -*- coding:utf-8 -*-
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
if not pushV or len(pushV) != len(popV):
return False
stack = []
for i in pushV:
stack.append(i)
while len(stack) and stack[-1] == popV[0]:
stack.pop()
popV.pop(0)
if len(stack):
return False
return True

1.判断两个序列长度是否相等,压入序列是否为空

2.辅助栈: 如果不是要出栈的元素就压入,否则就弹出。

3.pop(0):从列表的头部开始弹出

4.最后如果辅助栈没有清空,说明序列不行



举报

相关推荐

0 条评论