Accepted Clean Java Solution


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

Log in to reply
 

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