0ms cpp solution simpler than two stack structure


  • 0
    L

    class Stack {
    private:
    queue<int> myque;
    public:
    // Push element x onto stack.

    void push(int x) {
        if(myque.empty()){
            myque.push(x);
            return;
        }
        myque.push(x);
        for(int i=0;i<myque.size()-1;i++){
            myque.push(myque.front());
            myque.pop();
        }
    }
    
    // Removes the element on top of the stack.
    void pop() {
        myque.pop();
    }
    
    // Get the top element.
    int top() {
        return myque.front();
    }
    
    // Return whether the stack is empty.
    bool empty() {
        return myque.empty();
    }
    

    };


Log in to reply
 

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