-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path946.cpp
47 lines (45 loc) · 1.47 KB
/
946.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
36
37
38
39
40
41
42
43
44
45
46
47
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> temp;
int j = 0;
ios::sync_with_stdio(false);
cin.tie(nullptr);
for(int i = 0; i < pushed.size() ; ++i)
{
temp.push(pushed[i]);
while(!temp.empty() and temp.top() == popped[j])
{
temp.pop();
++j;
}
}
return temp.empty() ;
}
};
__________________________________________________________________________________________________
sample 9044 kb submission
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
class Solution {
public:
bool validateStackSequences(std::vector<int>& pushed, std::vector<int>& popped) {
int pu = 0;
int po = 0;
std::stack<int> s;
while (true) {
if (!s.empty() && po < popped.size() && s.top() == popped[po]) {
s.pop();
++po;
} else if (pu < pushed.size()) {
s.push(pushed[pu++]);
} else if (po == popped.size()) {
return true;
} else {
return false;
}
}
}
};
__________________________________________________________________________________________________