题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

栈顶弹出后,切片,判断首项是否为新的栈顶

from re import T
class Solution:
    def IsPopOrder(self , pushV: List[int], popV: List[int]) -> bool:
        # write code here
        if sorted(popV)!=sorted(pushV):
            return False                                  
        while popV:
            v1=popV.index(pushV[-1])                       
            popV.pop(v1)
            if len(popV)<v1+1:
                return True                       
            cut=popV[v1:]
            cut=[pushV.index(i) for i in cut]
            if cut[0]==max(cut):               
                pushV.pop(-1)
                continue
            else:
                return False

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务