栈的压入,弹出序列

class Solution {
public:
    bool isPopOrder(vector<int> pushV,vector<int> popV) {
        stack<int> st;
        int n=pushV.size(),m=popV.size();
        if(n!=m)    return false;
        int idx=0;
        for (int i = 0; i < n; i ++ )
        {
            if(!st.empty()&&st.top()==popV[i])
                st.pop();
            else 
            {
                while(idx<n&&popV[i]!=pushV[idx])
                {
                    st.push(pushV[idx]);
                    idx++;
                }
                if(idx==n)  return false;
                idx++;
            }
        }
        return true;
    }
};
posted @ 2023-03-30 13:40  穿过雾的阴霾  阅读(15)  评论(0)    收藏  举报