【剑指offer】栈的压入、弹出序列

栈的压入、弹出序列

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

栈的经典问题,判断一个序列是不是栈的弹出序列。

import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int[] pushA, int[] popA) {
        Stack<Integer> stack = new Stack<>();
        int p = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);

            while (!stack.empty() && stack.peek() == popA[p]) {
                stack.pop();
                p++;
            }
        }

        return p == popA.length;
    }
}
全部评论

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
10 收藏 评论
分享
牛客网
牛客企业服务