无法播放?请 点击这里 跳转到Youtube
切换视频源:

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/

GitHub答案源代码请点击这里

class MyQueue {
    Deque<Integer> stack1;
    Deque<Integer> stack2;
    /** Initialize your data structure here. */
    public MyQueue() {
        stack1 = new ArrayDeque<>();
        stack2 = new ArrayDeque<>();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        stack1.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if (!stack2.isEmpty()) {
            return stack2.pop();
        }
        
        moveFromS1toS2();
        return stack2.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if (!stack2.isEmpty()) {
            return stack2.peek();
        }
        
        moveFromS1toS2();
        return stack2.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();   
    }
    
    private void moveFromS1toS2() {
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
    }
}