my c++ solution


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

Log in to reply
 

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