AcWing 42. 栈的压入、弹出序列
原题链接
简单
作者:
greg666
,
2019-03-02 12:24:45
,
所有人可见
,
阅读 1057
bool isPopOrder(vector<int> pushV,vector<int> popV) {
if (pushV.size() != popV.size()) {
return false;
}
int n = pushV.size();
stack<int> myStack;
int i = 0, j = 0;
while (i < n) {
int cur = popV[i];
// check stack
if (!myStack.empty() && myStack.top() == cur) {
myStack.pop();
i++;
continue;
}
// check vector
while (j < n && pushV[j] != cur) {
myStack.push(pushV[j]);
j++;
}
if (j == n) {
return false;
}
i++;
j++;
}
return i == n;
}