# 剑指offer31： 栈的压入、弹出序列

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190619165005455.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2phY2tpZV9vMm8y,size_16,color_FFFFFF,t_70)

```python
# -*- 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
```

1.判断两个序列长度是否相等，压入序列是否为空

2.辅助栈： 如果不是要出栈的元素就压入，否则就弹出。

3.pop(0):从列表的头部开始弹出

4.最后如果辅助栈没有清空，说明序列不行


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qiangrens-organization.gitbook.io/qkd90/java-kai-fa/jian-zhi-offer-zhuan-xiang-tu-ji-ban/jian-zhi-offer31-zhan-de-ya-ru-tan-chu-xu-lie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
