题解 | #栈的压入、弹出序列#
栈的压入、弹出序列
http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106
# -*- coding:utf-8 -*-
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
# result = True
l = []
j = 0 # 用于标识弹出序列的位置
for i in range(len(pushV)): # 思路:遍历pushV的元素压入l,每次压入后进行判断l栈顶是否与popV当前位置元素相同
l.append(pushV[i])
# while 条件,判断辅助栈的栈顶元素是否等于popV[j]
while l and l[-1] == popV[j]:
# 符合条件则辅助栈弹出栈顶元素
l.pop()
j += 1
return l == [] # 根据l是否为空判断输出结果
''' while j < len(pushV):
if l and l[-1] == popV[j]:
l.pop()
j += 1
else:
l.append(pushV[i])
i += 1
if l == []:
return True
else:
return False'''
''' l.append(pushV.pop())
for i in range(len(l)):
if l[i] != popV:
result = False
return result'''