栈的压入、弹出序列

题目 栈的压入、弹出序列

image-abaaa9f9

思路分析

不断进行压栈操作

如果发现栈顶元素与输出序列的当前比对位置相同 就出栈

最后如果是空 就说明是符合的

如果最后不是空 就说明不可能是该顺序

出栈判断-3e42e2b7

代码实现

class Solution {

public:

    bool isPopOrder(vector<int> pushV,vector<int> popV) {

        if(pushV.size()!=popV.size())

            return false;

        stack<int> stk;

        int i=0;

        for(auto x:pushV){

            stk.push(x);

            while(stk.size() && stk.top()==popV[i]){

                stk.pop();

                i++;

            }

        }

        return stk.empty();

    }

};

同类题型

视频讲解


⬅️ 最长合法括号子串 🏠 00-刷题理模型 ➡️ (待做)火车进出栈问题