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

栈的压入、弹出序列

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();
    }
}
全部评论

相关推荐

07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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