Super easy beats 80% Java solution


  • 0
    class MyStack {
        Queue<Integer> q = new ArrayDeque();
        // Push element x onto stack.
        public void push(int x) {
            q.add(x);
            int size = q.size();
            while (size > 1) {
                q.add(q.poll());
                size--;
            }
        }
    
        // Removes the element on top of the stack.
        public void pop() {
            q.poll();
        }
    
        // Get the top element.
        public int top() {
            return q.peek();
        }
    
        // Return whether the stack is empty.
        public boolean empty() {
            return q.isEmpty();
        }
    }
    

Log in to reply
 

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