Share my solution


  • 0
    S

    class MyQueue {
    private Stack<Integer> now=new Stack<Integer>();
    private Stack<Integer> or=new Stack<Integer>();
    // Push element x to the back of queue.
    public void push(int x) {
    or.push(x);

    }
    
    // Removes the element from in front of queue.
    public void pop() {
         while(!or.isEmpty())
        {
            now.push(or.peek());
            or.pop();
        }
        now.pop();
         while(!now.isEmpty())
        {
            or.push(now.peek());
            now.pop();
        }
        
    }
    
    // Get the front element.
    public int peek() {
        int a=0;
        while(!or.isEmpty())
        {
            now.push(or.peek());
            or.pop();
        }
        a=now.peek();
    while(!now.isEmpty())
        {
            or.push(now.peek());
            now.pop();
        }
        return a;
    }
    
    // Return whether the queue is empty.
    public boolean empty() {
        return or.isEmpty();
    }
    

    }


Log in to reply
 

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