题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

借助栈,边push边pop

public boolean IsPopOrder(int [] pushA,int [] popA) {
        if(pushA.length!=popA.length)return false;
        Deque st = new LinkedList();
        int j=0;
        for(int i=0;i<pushA.length;i++){
            while(j<pushA.length&&(st.isEmpty()||st.peek()!=popA[i])){
                st.push(pushA[j]);
                j++;
            }//当栈顶等于pop或者遍历完push都没找到pop元素时结束while循环
            if(st.peek()==popA[i]){
                st.pop();
            }else{
                return false;//说明push里没有pop[i]
            }
        }
        return true;
全部评论

相关推荐

墨西哥大灰狼:如果你的校友卤馆还在的话,他肯定会给你建议的,可是卤馆注销了@ 程序员卤馆
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务