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

栈的压入、弹出序列

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

用一个栈模拟:


public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
      Stack<Integer> s = new Stack();
        int i = 0 , j = 0;
        while(i < pushA.length){
            if(pushA[i] == popA[j]){
                i++;
                j++;
                while(!s.isEmpty() && popA[j] == s.peek()){//看有没有符合加入就弹出
                    s.pop();
                    j++;
                }
            }else{
                s.push(pushA[i]);
                i ++;
            }
        }
        return s.isEmpty();
    }
}
全部评论

相关推荐

大摆哥:刚好要做个聊天软件,直接让你帮他干活了
点赞 评论 收藏
分享
挣K存W养DOG:他的价值在于把他家里积攒的财富回馈给社会
点赞 评论 收藏
分享
deepseek给的答案,自留一下
野猪不是猪🐗:《产品经理》
投递美团等公司9个岗位 > 美团求职进展汇总
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务