用栈解决的相关问题

1.已知某个栈的压栈序列,判断某个序列是不是该压栈序列的弹出序列。

 public boolean IsPopOrder(int [] pushA,int [] popA) {
        if(pushA.length==0) return false;
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0,j=0;i<pushA.length;i++){
            stack.push(pushA[i]);
            while(j<popA.length&&stack.peek()==popA[j]){
                stack.pop();
                j++;
            }
            
        }
        return stack.isEmpty();
}

 

posted @ 2019-04-15 20:39  LeeJuly  阅读(319)  评论(0)    收藏  举报