天天看点

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

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        stack<int> st;
        int index = 0;
        for(int i=0;i<pushV.size();i++){
            st.push(pushV[i]);
            while(index<popV.size() && st.top()==popV[index]){
                st.pop();
                index++;
            }
        }
        return st.empty();
    }
};
           

继续阅读