其他分享
首页 > 其他分享> > 剑指 Offer 31. 栈的压入、弹出序列

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

作者:互联网

之前写的又不会了。难过。用一个栈来模拟进出序列,最后模拟栈为空,返回true。

算法复杂度

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

标签:popped,出栈,压入,Offer,int,31,stack,isEmpty,复杂度
来源: https://blog.csdn.net/zhazha_hui/article/details/113028144