How to solve the problem of push-in and pop-up sequence of C++ stack
Editor to share with you how C++ to solve the stack push-and-pop sequence problem, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Next, let's go to know it!
# define _ CRT_SECURE_NO_WARNINGS 1#include#includeusing namespace std;#include#include/* question requires: enter two integer sequences. The first sequence represents the push-in order of the stack. Please determine whether the second sequence is the pop-up order of the stack. Assume that all the numbers pushed into the stack are not equal. For example, sequence 1, 2, 3, 4, 5 is the push-in order of a stack, and sequence 4, 5, 5, 5, 5, 4, 5, 5, 5, 5, 4, 5, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 4, 5, 4, 4, 5, 4, 5, 4, 4, 5, 4, 5, 4, 5, 4, 5, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 (note: the length of the two sequences is equal) * / / * solution: use a stack to insert the elements in pushV in turn. If the top element of the stack is equal to the pos element in popV, then pop the top element of the stack and let pos point to the end of element insertion in the next element pushV. If the stack is empty, the condition is met, otherwise it is not satisfied. * / class Solution {public: bool IsPopOrder (vector pushV, vector popV) {stack s; if (pushV.size ()! = popV.size ()) {return false;} int I = 0; int j = 0; while (I < pushV.size ()) {s.push (PushV [I + +]) While (! s.empty () & & s.top () = = popV [j]) {s.pop (); jobless;}} if (s.empty ()) {return true;} else {return false }; int main () {vectorv1 {1, 2, 3, 4 vectorv2 5}; Solution s; int a=s.IsPopOrder (v1, v2); cout