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

栈的压入、弹出序列

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

import java.util.ArrayList;
import java.util.Stack;


public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer>st = new Stack<Integer>();
        int i=0,j=0;
        while(j<popA.length && i<pushA.length){
            while(j<popA.length && i<pushA.length && popA[j]!=pushA[i]){
                st.add(pushA[i]);
                i++;
            }
            if(i<pushA.length) st.add(pushA[i++]);
            while(st.size()>0 && popA[j]==st.peek()){
                st.pop();
                j++;
            }
        }
        if(st.size()==0) return true;
        return false;
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 16:22
点赞 评论 收藏
分享
积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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