class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { // 压栈入栈的数字都不相同 /* 就是说,压和出我们模拟一下 1 2 3 > 4 1 2 > 3 */ // 通过模拟的方式,如果相同就跳过,如果不同那么就比较栈顶或者一直往后找 int index_in = 0; // 代表的是压入栈的点 int index_out = 0; stack<int> st_; while(index_in<pushV.size() && index_out<popV.size()) { if(pushV[index_in]==popV[index_out]) { index_in++; index_out++; } else if(!st_.empty() && popV[index_out]==st_.top()) { st_.pop(); index_out++; } else { st_.push(pushV[index_in++]); } } while(!st_.empty() && index_out<popV.size()) { if(st_.top()==popV[index_out]) { index_out++; st_.pop(); } else { return false; } } if(!st_.empty() || index_out!=popV.size()) { return false; } return true; } };