My Java solution with 2 stacks


  • 4
    C
    class MyQueue {
        Stack<Integer> push = new Stack<Integer>(); // this stack is used for pushing new elements
        Stack<Integer> pop = new Stack<Integer>(); // this stacks is used for popping old elements
        
        // Push element x to the back of queue.
        public void push(int x) {
            push.push(x);
        }
    
        // Removes the element from in front of queue.
        public void pop() {
            if (! pop.empty()) {
                pop.pop();
                return;
            }
            
            while (! push.empty()) {
                pop.push(push.pop());
            }
            
            pop.pop();
        }
    
        // Get the front element.
        public int peek() {
            if (! pop.empty()) {
                return pop.peek();
            }
            
            while (! push.empty()) {
                pop.push(push.pop());
            }
            
            return pop.peek();
        }
    
        // Return whether the queue is empty.
        public boolean empty() {
            return push.empty() && pop.empty();
        }
    }

  • 0
    Y

    great solution, with very intuitive naming of the "push" and "pop" stacks.


Log in to reply
 

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