剑指 Offer 31. 栈的压入、弹出序列
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
int index=0;
stack<int>st;
for(int i=0;i<pushed.size();i++)
{
st.push(pushed[i]);
while(!st.empty())
{
int x=st.top();
if(x==popped[index])
{
index++;
st.pop();
}
else break;
}
}
if(st.empty())
{
return true;
}
else return false;
}
};