My Java Solution only edit the queue's push method


  • 0
    K
    class MyStack {
    
    private Queue<Integer> mQueue = null;
    
    MyStack() {        
        mQueue = new ArrayDeque<Integer>();
    }
    
    // Push element x onto stack.
    public void push(int x) {
        Queue<Integer> tmp = new ArrayDeque<>();
        tmp.add((Integer)x);
        int len = mQueue.size();
        for (int i = 0; i < len; i++) {
            tmp.add(mQueue.element());
            mQueue.poll();
        }
        mQueue = tmp;
    }
    
    // Removes the element on top of the stack.
    public void pop() {
        mQueue.remove();
    }
    
    // Get the top element.
    public int top() {
        return mQueue.peek();
    }
    
    // Return whether the stack is empty.
    public boolean empty() {
        return mQueue == null ? true : mQueue.isEmpty();
    }
    

    }



  • 0
    This post is deleted!

Log in to reply
 

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