Java AC solution with two stacks


  • 3
    W
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    
    public MyQueue(){
        stack1 = new Stack<Integer>();
        stack2 = new Stack<Integer>();
     }
    
    // Push element x to the back of queue.
    public void push(int x) {
        while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        stack1.push(x);
        while(!stack2.isEmpty()){
            stack1.push(stack2.pop());
        }
    }
    
    // Removes the element from in front of queue.
    public void pop() {
        stack1.pop();
    }
    
    // Get the front element.
    public int peek() {
        return stack1.peek();
    }
    
    // Return whether the queue is empty.
    public boolean empty() {
        return stack1.isEmpty();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.