C++/代码:
class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { if (pushV.size() != popV.size()) return false; stack<int> stk; //定义一个栈 int i = 0; //定义第i个栈 for (auto x : pushV) { //枚举pushV当中的每一个数 stk.push(x); //插入x while (stk.size() && stk.top() == popV[i]) { stk.pop(); //一样则将当前栈顶元素弹出,不一样则继续枚举,并将x插入栈 i ++; } } return stk.empty(); //stk.empty()表示,为空则为真,不空则为假 } };