P134、面试题22:栈的压入、弹出序列

题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1、2、3、4、5是某栈的压栈序列,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该栈序列的弹出序列。

思路:如果下一个弹出的数字刚好是栈顶数字,那么直接弹出。如果下一个弹出的数字不在栈顶,我们把压栈序列中还没有入栈的数字压入辅助栈,直到把下一个需要弹出的数字压入栈顶为止。如果所有的数字,那么该序列不可能是一个弹出序列。
 
测试用例:
1)功能测试(输入的两个数组含有多个数字或者只有1个数字,第二个数组是或者不是第一个数组表示的压入序列对应的栈的弹出序列);
2)特殊输入测试(输入两个null指针)。
 
代码实现:
package com.yyq;

import java.util.Stack;

/**
 * Created by Administrator on 2015/9/16.
 */
public class StackPushPopOrder {
    public static boolean isPopOrder(int[] pPush, int[] pPop){
        boolean bPossible = false;
        if (pPop == null || pPush == null)
            return bPossible;
        int pslen = pPush.length;
        int polen = pPop.length;
        int i = 0;
        int j = 0;
        Stack<Integer> stackData = new Stack<>();
        if (pPush != null && pPop != null && pslen == polen && pslen > 0){
            while(i < polen){
                while ((stackData.empty() || stackData.peek() != pPop[i]) && j < pslen){ //如果存在相同的数据那又怎么处理呢?
                    stackData.push(pPush[j]);
                    j++;
                }
                if (stackData.peek() != pPop[i])
                    break;
                stackData.pop();
                i++;
            }
            if (stackData.empty() && i == polen)
                bPossible = true;
        }
        return bPossible;
    }


    // ====================测试代码====================
    public static void Test(String testName, int[] pPush, int[] pPop, boolean expected)
    {
        if(testName != null)
            System.out.println(testName+" begins: ");
        if(isPopOrder(pPush, pPop) == expected)
            System.out.println("Passed.");
        else
            System.out.println("failed.");
        System.out.println();
    }

    public static void Test1()
    {
        int push[] = {1, 2, 3, 4, 5};
        int pop[] = {4, 5, 3, 2, 1};
        Test("Test1", push, pop, true);
    }

    public static void Test2()
    {
        int push[] = {1, 2, 3, 4, 5};
        int pop[] = {3, 5, 4, 2, 1};
        Test("Test2", push, pop, true);
    }

    public static void Test3()
    {
        int push[] = {1, 2, 3, 4, 5};
        int pop[] = {4, 3, 5, 1, 2};
        Test("Test3", push, pop, false);
    }

    public static void Test4()
    {
        int push[] = {1, 2, 3, 4, 5};
        int pop[] = {3, 5, 4, 1, 2};

        Test("Test4", push, pop, false);
    }

    // push和pop序列只有一个数字
    public static void Test5()
    {
        int push[] = {1};
        int pop[] = {2};

        Test("Test5", push, pop, false);
    }

    public static void Test6()
    {
        int push[] = {1};
        int pop[] = {1};

        Test("Test6", push, pop,true);
    }

    public static void Test7()
    {
        Test("Test7", null, null, false);
    }

    public static void main(String[] args){
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
    }
}
 
结果输出:
Test1 begins: 
Passed.
 
Test2 begins: 
Passed.
 
Test3 begins: 
Passed.
 
Test4 begins: 
Passed.
 
Test5 begins: 
Passed.
 
Test6 begins: 
Passed.
 
Test7 begins: 
Passed.
原文地址:https://www.cnblogs.com/yangyquin/p/4942099.html