面试题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

  

 

posted @ 2019-08-05 11:13  lililili——  阅读(202)  评论(0)    收藏  举报