C++ 0ms two stacks


  • 0
    class MyQueue {
    public:
        stack<int>t;
        stack<int>q;
        MyQueue() {}
        
        void push(int x) {
            t.push(x);
        }
        
        void exchange(){
            while(!t.empty()){
                q.push(t.top());
                t.pop();
            }
        }
        
        int pop() {
            int i;
            if(q.empty()) exchange();
            i=q.top();
            q.pop();
            return i;
        }
        
        int peek() {
            if(q.empty()) exchange();
            return q.top();
        }
        
        bool empty() {
            return q.empty()&&t.empty();
        }
    };
    

Log in to reply
 

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