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.
Solution:
Operation of the simulation stack
Create a stack variable that represents the stack;
If the stack is empty, or stack.top () ==popv[i],stack.pop
Otherwise, Stack.push (PUSHV "Inpos])
bool IsPopOrder(vector<
int
> pushV, vector<
int
> popV) {
-
int
size1 = Pushv.size ();
-
int
size2 = Popv.size ();
if
(size1 != size2||size1==
0
)
return
false
;
stack<
int
> st;
int
inpos =
0
;
for
(
int
i =
0
; i<size2;) {
-
if
(!st.empty () && st.top () = = Popv[i]) {
-
st.pop ();
-
i++;
-
}
-
else
{
-
if
(Inpos < size1)
-
st.push (pushv[inpos++]);
else
return
false
;
-
}
-
}
-
return
inpos = = Size1&&st.empty ();
}
Stack push-in, pop-up sequence