验证栈序列,我们只需要模拟栈就可以了,我们先插入一个数据,然后判断这个数据跟要验证的是否相同,如果相同,我们和把顶端的出栈,然后验证下一个,最后判断栈是不是空的就行。
class Solution { public: bool validateStackSequences(vector<int>& pushed, vector<int>& popped) { stack<int> st; int v = 0; for(int i = 0;i<pushed.size();i++) { st.push(pushed[i]); while(!st.empty() && st.top() == popped[v]) { v++; st.pop(); } } return st.empty(); } };