"Title description"Enter a sequence of two integers, and the first sequence represents the stacking order of the stack, judging whether the second sequence is the pop-up order for the stack. Assume that all the numbers that are pressed into the stack are not equal. For example, the sequence 1,2,3,4,5 is the indentation order of a stack, and the sequence 4,5,3,2,1 is a pop-up sequence corresponding to the stack sequence, but 4,3,5,1,2 is not likely to be the pop-up sequence of the stack sequence. "Code Implementation"
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 BOOLIspoporder (vector<int> PUSHV, vector<int>POPV) {7 if(pushv.size () = =0)8 return false;9Vector <int>Vec;Ten for(inti =0, j =0; I < pushv.size (); i++) One { AVec.push_back (Pushv[i]);//re-simulate the initial stack process - while(J < Popv.size () && vec.back () = =Popv[j]) - { theVec.pop_back ();//re-simulate the initial stack-out process -J + +; - } - } + //successful simulation succeeds, the element should all pop up, return to true, otherwise there will be no element eject, resulting in non-null, return to False - returnvec.empty (); + } A at intMainvoid) - { -vector<int> pushlist{1,2,3,4,5 }; -vector<int> poplist{4,3,5,1,2 }; -cout << Ispoporder (pushlist, poplist) <<Endl; -System"Pause"); in return 0; -}
Stack push-in, pop-up sequence