-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
35 lines (33 loc) · 899 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> s;
auto it = pushed.begin();
for (int v : popped) {
while (s.empty() || s.top() != v) {
if (it == pushed.end()) return false;
s.push(*it++);
}
s.pop();
}
return true;
}
};
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
int m = pushed.size(), n = popped.size();
int i = 0, j = 0;
stack<int> stk;
while (i < m || j < n) {
if (stk.empty() || stk.top() != popped[j]) {
if (i == m) return false;
stk.push(pushed[i++]);
} else {
++j;
stk.pop();
}
}
return true;
}
};