My JAVA Solution beats 88.68%


  • 0
    R

    class MyQueue {
    Stack stack1 = new Stack();
    Stack stack2 = new Stack();
    // Push element x to the back of queue.
    public void push(int x) {
    if(stack1.empty()){
    stack1.add(x);
    }
    else{
    while(!stack1.empty()){
    stack2.add(stack1.pop());
    }
    stack1.add(x);
    while (!stack2.empty()){
    stack1.add(stack2.pop());
    }
    }
    }

    // Removes the element from in front of queue.
    public void pop() {
        stack1.pop();
    }
    
    // Get the front element.
    public int peek() {
        return  (int) stack1.peek();
    }
    
    // Return whether the queue is empty.
    public boolean empty() {
        return stack1.empty();
    }
    

    }


Log in to reply
 

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