题目:946. 验证栈序列
思路:栈
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int index = 0;
for(int i = 0;i < pushed.size();++i){
st.push(pushed[i]);
while(index < popped.size() && !st.empty() && popped[index] == st.top()){
index++;
st.pop();
}
}
if(index == popped.size()){
return true;
}else{
return false;
}
}
};