Validate Stack Sequences

第41天。

今天的题目是Validate Stack Sequences:

简单题,直接模拟就好了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int i = 0;
for(auto &t: popped) {
if (!st.empty() && st.top() == t) {
st.pop();
} else {
while(i < pushed.size() && pushed[i] != t) {
st.push(pushed[i]);
i++;
}
if (pushed.size() == i) return false;
i++;
}
}
return true;
}