面试题31:栈的压入、弹出序列
# -*- coding:utf-8 -*-
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
# pushV, popV 是2个整数序列
# 我们首先要有一个栈,也就是一个列表
# 压入:按照push的方式压入栈
# 弹出: 弹出时候需要循环判断是否需要弹出
# 如何判断弹出的时机:在刚刚压入的时候就判断
# 判断需要弹出情况的条件,压入栈的顶部和弹出栈的顶部数据相等
if pushV == [] and len(pushV) != len(popV):
return None
stack = []
index = 0
for item in pushV:
stack.append(item)
while stack and stack[-1] == popV[index]:
stack.pop()
index += 1
if stack == []:
return True
else:
return False


浙公网安备 33010602011771号