每一步进行的操作有两种,① 将下一个数压入栈中 ② 将当前栈顶元素弹出
判断当前栈顶元素是否和下一个要输出的数是一样的
① 一样 => 必然会将当前栈顶元素弹出
② 不一样 => 必然会将输入序列的下一个元素加入栈中
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)
{
// cout << x << endl;
stk.push(x);
while (stk.size() && stk.top() == popV[i])
{
// cout << stk.top() << popV[i] << endl;
stk.pop();
// cout << i << endl;
i ++;
}
}
return stk.empty();
}
};