由于没有容量限制,直接模拟即可。
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int i = 0, j = 0;
while(j<pushed.size()){
while(st.empty()||st.top()!=popped[j]){
if(i==pushed.size()) return false;
st.push(pushed[i++]);
}
st.pop();
j++;
}
return true;
}
};