Title

#栈#单调栈#leetCode94.验证栈序列

 

 

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Deque<Integer> q = new LinkedList<>();
        int N = pushed.length;
        int j = 0;
        for(int x:pushed) {
            q.push(x);
            while(q.isEmpty()==false && q.peek()==popped[j]) {
                q.pop();
                ++j;
            }
        }
        return q.isEmpty();
    }
}

 

posted @ 2020-07-15 20:47  .geek  阅读(103)  评论(0编辑  收藏  举报