给两个数组pushed和poped,其中pushed为栈的压入序列,popped为栈的弹出序列,判断第一个序列是否是第一个序列的弹出序列。假设压入栈的所有数字均不相等。
分析:辅助栈判断。
class Solution
{
public:
bool validateStackSequences(vector<int>& pushed,vector<int>& popped)
{
stack<int> stk;
int i=0;
for(auto e:pushed)
{
stk.push(e);
while(!stk.empty() && stk.top()==popped[i])
{
stk.pop();
i++;
}
}
return stk.empty();
}
};