Stack and ArrayList solution


  • 0
    W

    public class MyQueue {

    Stack<Integer> push = new Stack<>();
    List<Integer> array = new ArrayList<>();
    int size = 0;
    int cur= 0;
    
    public MyQueue() {
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        push.push(x);
        array.add(x);
        size++;
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        size--;
        return array.get(cur++);
    }
    
    /** Get the front element. */
    public int peek() {
        return array.get(cur);
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return size == 0 ? true : false;
    }
    

    }


Log in to reply
 

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