www

导航

栈的压入、弹出序列

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

 

posted on 2019-02-28 18:57  www_practice  阅读(89)  评论(0编辑  收藏  举报