剑指offer(1)——栈的压入、弹出序列

题目:

    输入两个整数序列。其中一个序列表示栈的push 顺序,判断另一个序列有没有可能是对应的pop 顺序。
为了简单起见,我们假设push 序列的任意两个整数都是不相等的。
比如输入的push 序列是1、2、3、4、5,那么4、5、3、2、1 就有可能是一个pop 系列,但序列4、3、5、1、2 就不可能是push 序列1、2、3、4、5 的pop 序列。

解答:

//思路:先压入一个元素到辅助栈,如果栈元素不为空且栈顶元素等于弹出序列的第一个元素,则

// 弹出元素,弹出序列向后移,继续比较;否则,压入元素向后移,继续压入元素。。。。直到压入序列走完或者弹出序列走完
// ,此时看栈中元素个数是否为0

public boolean IsPopOrder(int[] pushA, int[] popA) {
if (null == pushA || null == popA) {
return false;
}
Stack<Integer> stack1 = new Stack<Integer>();
int pushLength = pushA.length;
int popLength = popA.length;
int i = 0;
int j = 0;
while (i < pushLength && j < popLength) {
stack1.push(pushA[i]);// 先压入一个元素到栈
while (!stack1.empty() && Objects.equals(popA[j], stack1.peek())) {
stack1.pop();// 相等则弹出
j++;// 弹出序列后移
}
i++;// 压栈序列继续向后移
}
return stack1.empty();
}

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

stack.peek()与stack.pop()方法的区别

相同点:都返回栈顶的值。

不同点:peek 不改变栈的值(不删除栈顶的值),pop会把栈顶的值删除。

源码分析:参考:https://blog.csdn.net/alan_gaohaodong/article/details/79278171

(1)这是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);
    }

  这是上述最后一行代码elementAt(len-1)方法的源码:

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

这是上述elementData(index)方法的源码:

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

 当看到elementData[index]时就会发现java里的栈的实现是用数组来实现的。再返回去看peek()方法的源码会发现只是把数组的最后一个元素(就是栈顶)返回来了,而没有做删除。

(2)这是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;
    }

这是removeElementAt(len-1)方法的源码

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 */
    }

上述的removeElementAt(len-1)方法的源码中,倒数第二行的代码的地方是将数组中存放的个数减少一个,然后将最后一个元素

设置位null值。也就验证了pop是会移除最后一个元素(也就是栈顶元素)的。

原文地址:https://www.cnblogs.com/baimh/p/11298658.html