栈的压入弹出序列

public class 栈的压入弹出序列
{
    // 判断一个弹出序列是否为该压栈序列对应的一个弹出序列
    public static boolean ispopAOrder(int[] pushA, int[] popA)
    {
        boolean res = false;
        // 辅助栈
        Stack<Integer> pushAStack = new Stack<Integer>();
        // 特殊值考虑
        if (pushA == null || popA == null || pushA.length != popA.length)
        {
            return res;
        }
        // 通过比较popA[i]与pushA[i],如果相同则i++,
        int length = pushA.length;
        int i = 0, j = 0;
        while (i < length && j < length)
        {
            pushAStack.add(pushA[i]);
            // 如果栈顶元素与popA[i]相同,则继续比较栈中的下一位
            while (pushAStack.peek() == popA[j])
            {
                // popA
                pushAStack.pop();
                // 后移
                j++;
                if (j == length)
                {
                    return true;
                }
            }
            i++;

        }
        return res;
    }

}

原文地址:https://www.cnblogs.com/qingtianBKY/p/8250640.html