Wins the offer (1) - stack push, pop sequence

topic:

    Two input sequence of integers. Wherein the sequence represents a stack push sequence, another sequence is determined there may be a corresponding sequence of pop.
For simplicity, we assume that any two integers push sequences are not equal.
Push sequence such as input is 1,2,3,4,5, 4,5,3,2,1 so there may be a pop series, but can not be a push sequence 4,3,5,1,2 sequence pop sequence 1,2,3,4,5.

answer:

@ Idea: to be pressed into a secondary element stack, if the stack is not empty element equal to pop the top element and the first element of the sequence, the

// pop elements, pop-up sequence moves back, the comparison continues; otherwise, the press-fit element moves back and continue to press the elements. . . . Sequence until the completed press-pop sequence or finish
//, this time to see whether the number of elements in the stack is 0

Boolean IsPopOrder public (int [] PUSHA, int [] POPA) {
IF (== null || PUSHA POPA == null) {
return to false;
}
Stack <Integer> = Stack1 new new Stack <Integer> ();
int = pushLength pushA.length;
int = popLength popA.length;
int I = 0;
int J = 0;
the while (I <pushLength && J <popLength) {
stack1.push (PUSHA [I]); // element to be pressed into a stack
the while (stack1.empty () && Objects.equals (POPA [J], stack1.peek ())!) {
stack1.pop (); // are equal pop
j ++; // pop-up sequence shifted
}
I ++; / / push sequence continues to move rearwardly
}
return stack1.empty ();
}

------------------

Difference stack.peek () and stack.pop () method

The same point: the return value of the top of the stack.

Different points: peek does not change the value of the stack (do not delete the value of the stack), the value of the stack of pop will be deleted.

Source Analysis: Reference: https: //blog.csdn.net/alan_gaohaodong/article/details/79278171

(1) a method which is the source of the JDK peek

   /**
     * Looks at the object at the top of this stack without removing it
     * from the stack.
     *
     * @return  the object at the top of this stack (the last item
     *          of the <tt>Vector</tt> object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E peek() {
        int     len = size();


        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

  This is the last line of code above the source elementAt (len-1) Method:

  public synchronized E elementAt(int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
        }
        return elementData(index);
    }

This is the source above elementData (index) method:

    E elementData(int index) {
        return (E) elementData[index];
    }

 When you see elementData [index] will find in the realization of java stack is an array to achieve. And then return to see a peek () method of the source code will find only the last element of the array (that is top of the stack) returns came, but do not delete.

(2) a method which is the source of the JDK pop

  /**
     * Removes the object at the top of this stack and returns that
     * object as the value of this function.
     *
     * @return  The object at the top of this stack (the last item
     *          of the <tt>Vector</tt> object).
     * @throws  EmptyStackException  if this stack is empty.
     */
    public synchronized E pop() {
        E       obj;
        int     len = size();
        obj = peek();
        removeElementAt(len - 1);
        return obj;
    }

This is removeElementAt source (len-1) Method

public synchronized void removeElementAt(int index) {
        modCount++;
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        else if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        int j = elementCount - index - 1;
        if (j > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        elementCount--; //注意这里是减减操作(- -),连在一起了,看不清
        elementData[elementCount] = null; /* to let gc do its work */
    }

The method of the above-described source removeElementAt (len-1), the local code is the inverse of the second line is to reduce the number stored in the array a, and then the last element

Set bit null value. It is verified pop removes the last element (that is, the top element) of.

Guess you like

Origin www.cnblogs.com/baimh/p/11298658.html