zoukankan      html  css  js  c++  java
  • 【剑指Offer】21栈的压入、弹出序列

    题目描述

    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

    时间限制:1秒;空间限制:32768K;本题知识点:栈

    解题思路

    PUSH 1 2 3 4 5

    POP  4 5 3 2 1 正确

             4 3 5 1 2 错误

             4 3 5 2 1 正确 (4和43看作一样的情况)

    # -*- coding:utf-8 -*-
    class Solution:
        def IsPopOrder(self, pushV, popV):
            # write code here
            if not pushV or len(pushV) != len(popV):
                return False
            stack = []
            for i in pushV:
                stack.append(i)
                while len(stack) and stack[-1] == popV[0]:
                    stack.pop()
                    popV.pop(0)
            if len(stack):
                return False
            return True
  • 相关阅读:
    力扣题解 125th 验证回文字符串
    力扣题解 242th 有效的字母异位词
    力扣题解 387th 字符串中的第一个唯一字符
    Ubuntu
    Ubuntu
    Ubuntu
    ubuntu
    go-vscode-ubuntu20.04安装部署
    go-vscode-windows安装部署
    2020年任务
  • 原文地址:https://www.cnblogs.com/yucen/p/9912039.html
Copyright © 2011-2022 走看看