AcWing 42. 栈的压入、弹出序列
原题链接
简单
作者:
daniellee
,
2019-04-10 20:39:40
,
所有人可见
,
阅读 845
C++ 代码
class Solution {
public:
bool isPopOrder(vector<int> pushV,vector<int> popV) {
if (pushV.size()!=popV.size()) return false;
if (pushV.size()==0 && popV.size()==0) return true;
int n = pushV.size();
int m = popV.size();
stack<int> st;
int i=0;
int j=0;
for(;j<m;j++){
int k = popV[j];
for (;i<n;i++){
if (st.empty()){
st.push(pushV[i]);
}
else if (k==st.top()){
st.pop();
break;
}
else{
st.push(pushV[i]);
}
}
if (!st.empty()){
if (k==st.top()){
st.pop();
continue;
}
}
}
// cout<<st.size()<<" "<<j<<" "<<i;
if (st.empty() ) return true;
else return false;
}
};