栈的压入,弹出序列
class Solution {
public:
bool isPopOrder(vector<int> pushV,vector<int> popV) {
stack<int> st;
int n=pushV.size(),m=popV.size();
if(n!=m) return false;
int idx=0;
for (int i = 0; i < n; i ++ )
{
if(!st.empty()&&st.top()==popV[i])
st.pop();
else
{
while(idx<n&&popV[i]!=pushV[idx])
{
st.push(pushV[idx]);
idx++;
}
if(idx==n) return false;
idx++;
}
}
return true;
}
};
有帮助的话可以点个赞,我会很开心的~