剑指offer-栈的压入、弹出序列(python)

这题leetcode也有,还是设置一个新的stack,和popV相比,一起pop,有一点注意就是while循环的时候,一定要加上stack是否为空这个选项,不然过不了。

# -*- coding:utf-8 -*-
class Solution:
    def IsPopOrder(self, pushV, popV):
        # write code here
        if not pushV or not popV:
            return False
        stack=[]
        for i in range(len(pushV)):
            stack.append(pushV[i])
            while  stack andstack[-1]==popV[0]:
                stack.pop()
                popV.pop(0)
        return stack==[]
发布了69 篇原创文章 · 获赞 46 · 访问量 5269

猜你喜欢

转载自blog.csdn.net/qq_42738654/article/details/104236843