0 ms C++ solution


  • -4
    C
    class Stack {
    public:
        // Push element x onto stack.
        queue<int> q[2];
        int alter=0;
        void push(int x) {
            q[alter].push(x);
        }
        // Removes the element on top of the stack.
        void pop(void) {
            for (int i=0;i<q[alter].size()-1;i++)
            {
                q[1-alter].push(q[alter].front());
                q[alter].pop();
            }
    
            q[alter].pop();
            alter=1-alter;
        }
    
        // Get the top element.
        int top(void) {
            return q[alter].back();
        }
    
        // Return whether the stack is empty.
        bool empty(void) {
            return q[alter].empty();
        }
    };

  • 0
    M

    in "top", q[alter].back() is not allowed according to the problem statement


Log in to reply
 

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