【剑指offer】栈的压入、弹出序列(python)
1. 设一个栈来模拟压入弹出操作。
2. 设一个 popindex 扫描输出序列,判断栈顶元素是不是当前出栈序列 popSequence 的第一个元素。如果是的话就执行出栈操作并将 popSequence 后移一位,继续判断。
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.stack = []
def IsPopOrder(self, pushV, popV):
# write code here
popindex = 0
for i in range(len(pushV)):
self.stack.append(pushV[i])
while(popindex < len(pushV) and self.stack and self.stack[-1]==popV[popindex]):
self.stack.pop()
popindex += 1
return not self.stack
京公网安备 11010502036488号