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

栈的压入、弹出序列

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

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        int i = 0;//pushA的指针
        int j = 0;//popA的指针
        Stack<Integer> stack = new Stack<>();
        //先压栈,后判断
        stack.push(pushA[i]);
        while(i < pushA.length && j < popA.length){
            if(stack.empty() || stack.peek() != popA[j]){//不相等
                i++;
                if(i == pushA.length){return false;}//i越界,说明都不匹配
                stack.push(pushA[i]);//再次压入一个
            }else{//相等
                stack.pop();//出栈
                j++;
            }
        }
        if(stack.empty()){
            return true;
        }
      return false;
    }
}
全部评论

相关推荐

07-10 11:08
门头沟学院 Java
Sairus:我注册都注册不了提醒我手机号二次啥的,果然对于人才推得就是快,像我投完了就没回音的
投递京东等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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