-
Notifications
You must be signed in to change notification settings - Fork 0
/
0946.validate-stack.cpp
35 lines (31 loc) · 957 Bytes
/
0946.validate-stack.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
/**
* @file 0946.validate-stack.cpp
* @author Ilya Savitsky (ipsavitsky234@gmail.com)
* @brief https://leetcode.com/problems/validate-stack-sequences/
* @version 0.1
* @date 2023-04-13
*
* @copyright Copyright (c) 2023
*
*/
#include <stack>
#include <vector>
class Solution {
public:
bool validateStackSequences(std::vector<int> &pushed,
std::vector<int> &popped) {
std::vector<int>::const_iterator popped_it = popped.cbegin();
std::vector<int>::const_iterator pushed_it = pushed.cbegin();
std::stack<int> pot_stack;
while (!(pushed_it == pushed.cend())) {
pot_stack.push(*pushed_it);
while (!pot_stack.empty() && pot_stack.top() == *popped_it &&
popped_it != popped.cend()) {
pot_stack.pop();
++popped_it;
}
++pushed_it;
}
return pot_stack.empty();
}
};