剑指 Offer 09. 用两个栈实现队列

剑指 Offer 09. 用两个栈实现队列

class CQueue {
    Deque<Integer> stack1 = new ArrayDeque<>();
    Deque<Integer> stack2 = new ArrayDeque<>();

    public CQueue() {

    }
    
    public void appendTail(int value) {
        stack1.push(value);
    }
    
    public int deleteHead() {
        if(stack1.isEmpty() && stack2.isEmpty()) return -1;
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()) stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
}

你可能感兴趣的:(#,剑指offer,算法)