To prove safety offer stack push and pop

Description Title
input two integers sequences, a first sequence representing whether the stack is pressed into the sequence, the second sequence is determined may make the pop-up sequence for the 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)

 


Stack features: last out

 

Algorithms ideas:

1. The use of an auxiliary stack to simulate the operation of pushing and popping

2. Enter the elements of the sequence, the first stack, if the stack is not empty, you see the value of the top element and the pop-up sequence is the same,
the same put the top of the stack out of the stack, the current value of the future move a pop-up sequence, value as the pop-up sequence continues to determine the next is pressed into a sequence the same there.

Specifically: first the first element into the stack, here is 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 equal after the start of the stack, the stack is an element, it will pop back sequential shifted by one, until not equal,

3. In this way circulation Yazhan order traversal is complete, if the auxiliary stack is not empty, indicating that the sequences are not pop pop order stack.

From the analysis of the topic:

Stack order: 1,2,3,4,5

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

 

Proceed as follows:

1 into the auxiliary stack 1. First, when the stack 1 ≠ 4, continued stack 2

2. The stack 2 ≠ 4, continue to stack 3

3. The stack 3 ≠ 4, 4 continue to stack

4 = 4. At this time, the stack 4, the stack 4, a pop-up sequence back, in this case the auxiliary stack 5 inside 1,2,3 ,,

5. At this time, the stack 3 ≠ 5, 5 continue to push

5 5 = 6. In this case the stack, the stack 5, a pop-up sequence back, in this case the auxiliary stack 3 which is 2,3 ,,

7. At this time, the stack 3 = 3, the three stacks, there is also an auxiliary stack 2

8. At this time, the stack 2 = 2, the two stacks, there is also an auxiliary stack

9. At this time, the stack 1 = 1, to a stack, the stack is empty aid, indicating that the correct sequence is the sequence of the stack

Stack (): Method Description:

peek () represents the top of the stack view of the object, but does not remove it from the stack.

push (E item) is represented by the item onto the top of the stack.

pop () is represented by an object removable top of the stack, and the value of this function returns the object.

empty () indicates the test whether the stack is empty.

search (Object o) represents the return position of the object in the stack, to 1 as the base.

code show as below:

import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        if (pushA.length == 0 || popA.length == 0){
            return false;
        }
        int flag = 0;
        for (int i = 0; i < pushA.length; i++){
            stack.push(pushA[i]);
            while (!stack.isEmpty() && stack.peek() == popA[flag]){
                stack.pop();
                flag++;
            }
        }
        return stack.isEmpty();
    }
}

 

Guess you like

Origin www.cnblogs.com/hetaoyuan/p/11322423.html