给定入栈序列,判断一个串是否为出栈序列

剑指offer22:给定入栈序列,判断一个串是否为出栈序列
public static boolean isOutStackSequence(int[] Spush, int[] Spop) {
        if (Spush.length <= 0 || Spop.length <= 0 || Spush.length != Spop.length)
            return false;
        int len = Spush.length;
        Stack<Integer> s = new Stack<Integer>();
        int i=0,j=0;
        for (; i < len; i++) {
            while(s.isEmpty()||Spop[i]!=s.peek()){
                if(j<len){
                    s.push(Spush[j]);
                    j++;
                }else{
                    break;
                }
            }
            if(Spop[i]!=s.peek()) break;
                s.pop();
        }
        if(i<len)return false;
        else return true;
    }



原文地址:https://www.cnblogs.com/todayjust/p/5421585.html