0ms C++ solution avoided using .size( )


  • 4
    B
    class Stack {
        public:
        // Push element x onto stack.
        void push(int x) {
            if( q1.empty() ){ 
                q1.push(x);
                while (!q2.empty()){
                    q1.push(q2.front());
                    q2.pop();
                }
            }
            else{ 
                q2.push(x); 
                while (!q1.empty()){
                    q2.push(q1.front());
                    q1.pop();
                }
            }
        }
    
        // Removes the element on top of the stack.
        void pop() {
            if (!q1.empty()) { q1.pop(); }
            else{ q2.pop(); }
        }
    
        // Get the top element.
        int top() {
            if (!q1.empty()) { return q1.front(); }
            else{ return q2.front(); }
        }
    
        // Return whether the stack is empty.
        bool empty() {
            return q1.empty() && q2.empty();
        }
    private:
        queue<int> q1, q2;
    };

  • 0

    There is no benefit of using two queue instead of just using .size().


Log in to reply
 

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