剑指 Offer 31. 栈的压入、弹出序列

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer>  stack = new Stack<>();
        int index = 0;
        for(int x : pushed){
            stack.push(x);
            //while(popped[index] == stack.peek() &&  !stack.isEmpty()) 数组越界
            while(!stack.isEmpty() && popped[index] == stack.peek() ){
                stack.pop();
                index++;
            }
        }
        return stack.isEmpty();
    }
}

 

posted @ 2020-12-15 12:20  peanut_zh  阅读(70)  评论(0编辑  收藏  举报