Simple c++ solution, 0ms, easy-understand


  • 0

    class Queue {
    public:
    stack<int> s;
    stack<int> sub;

    void aStackb(stack<int>& a, stack<int>& b)
    {
        while(!a.empty())
        {
            b.push(a.top());
            a.pop();
        }
    }
    
    // Push element x to the back of queue.
    void push(int x) {
        aStackb(s, sub);
        s.push(x);
        aStackb(sub, s);
    }
    
    // Removes the element from in front of queue.
    void pop(void) {
        s.pop();
    }
    
    // Get the front element.
    int peek(void) {
        return s.top();
    }
    
    // Return whether the queue is empty.
    bool empty(void) {
        return s.empty();
    }
    

    };


Log in to reply
 

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