栈的压入弹出序列

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值