Wins the Offer (XXI): stack push, pop sequence

Wins the Offer (XXI): stack push, pop sequence

Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners

First, the primer

This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click: to prove safety Offer complete analytical exercises

Second, the title

Two input sequence of integers, the first sequence representing a pressed stack order, determines whether it is possible for the second sequence the order of the pop-up stack. All figures are not pushed onto the stack is assumed equal. Such as a sequence of a sequence of 1,2,3,4,5 is pressed into the stack, the push sequence is 4,5,3,2,1 sequence corresponding to a sequence of pop, but 4,3,5,1,2 it is impossible to push pop-up sequence of the sequence. (Note: the length of the two sequences are equal)

1, ideas

Before doing this problem, we need to know what the stack form into the stack change is noted that the value is: not only new element into the stack at the final stack. For example child, we now have three integer numbers 1,2,3 elements turn into the stack, which will be the order of the stack that?

The order will have the following five kinds:

  • Into 1,2,2, 3,2,1 out again, the order of the stack 321;
  • Intake, 1, 2 inlet, 2, 3 into, 3, the order of the stack 123;
  • Intake, feed 2, 2, 1, 3 inlet, 3, the order of the stack 213;
  • Intake, 1, 2 enter, into the 3, 3, 2, the order of the stack 132;
  • Intake, feed 2, 2, 3 into, 3, 1, the order of the stack 231.

Then is our algorithms, and borrow an auxiliary stack, push traversal order, will speak first placed on the stack, there are 1, and then determine the top element is not out of the first element of the stack order, here is 4, it is clear that 1 ≠ 4, so we continue to push until after the start of an equal stack, the stack element, moving backward one, until the stack will not equal the order, and so the cycle Yazhan order traversal is complete, If the secondary stack is not empty, indicating that the sequences are not pop pop order stack.

The above explanation if not really understand, I refer to a method for dynamic and detailed illustration of a big brother discharge steps explained below:

Blog address: https://blog.csdn.net/fanfan199312/article/details/91346039

1. Drawing order 1,2,3,4,5.

The stack order, 4,5,3,2,1

Problem solving process, we need to use an auxiliary stack, the stack according to the order value onto the stack pop up again, the order of comparison is equal to a pop out of the stack

(1) pop elements 4, then 4 before all the elements of the stack, four have been into the secondary stack. (1,2,3 without first stack, 4 how the stack, the stack is also not a), the auxiliary stack result, 1,2,3,4. Comparative auxiliary stack 4 and the element 4 is equal to pop the stack.

The stack order of  3, 4 , 5.

The stack order of 4 , 5,3,2,1

Auxiliary stack elements, 2, 3, 4  pop the top element 4

(2) the pull element 5, instead of the auxiliary stack top element 5, so that the sequence of elements from the stack before the stack 5 have, until it encounters 5

The stack order of  1,2,3 , 4, 5 .

The stack order of  4,5 , 3,2,1

The auxiliary stack, 2,3, 5 , 5 corresponding pop the top element

(3) the order of the stack 3 and the auxiliary top element 3, equal comparison, pop the top element 3.

Drawing order  2,3 , 4,5.

The stack order of  4,5,3 , 2,1

Auxiliary stack 2, 3  pop the top element 3

(4) the order of 2 and the auxiliary stack top element 2, equal comparison, pop the top element 2.

The stack order of  1,2 , 3,4,5.

The stack order of  4,5,3,2 , 1

Auxiliary Stack 1, 2 , 2 pop the top element

(5) the sequence 1 and the auxiliary stack top element 1, equal comparison, pop the top element 1,

The stack order of  1 , 2,3,4,5.

Stack the order of  4,5,3,2,1

Auxiliary stack empty 1 . Pop-top element 1.

At this time, the order of the stack, the stack order of traversal have been completed, the stack is empty, return.

FIG movable follows

Left and right side of the auxiliary stack Stack sequence comparison

2, programming

python2.7

Code implementation:

# -*- coding:utf-8 -*-
class Solution:
    def IsPopOrder(self, pushV, popV):
        # write code here
        if len(popV) == 0 or len(pushV) != len(popV):
            return False
        stackData = []
        for i in pushV:
            stackData.append(i)
            while len(stackData) and stackData[-1] == popV[0]:
                stackData.pop()
                popV.pop(0)
        if len(stackData):
            return False
        return True

AIMI-CN AI learning exchange group [1015286623] for more information on AI

Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!

Guess you like

Origin www.cnblogs.com/aimi-cn/p/11424741.html