My c++ solution


  • -4
    L
    class Stack {
        queue<int> myqueue;
    public: 
        // Push element x onto stack.
        void push(int x) {
            
            if(myqueue.empty()) myqueue.push(x);
            else{
            	int* begin = &myqueue.front();
            	int* end = begin+myqueue.size();
            	vector<int> v(begin,end);
    
            	while(!myqueue.empty()) myqueue.pop();
            	myqueue.push(x);
    
            	for(int j=0;j<v.size();j++)
            		myqueue.push(v[j]);
    
            }
        }
    
        // Removes the element on top of the stack.
        void pop() {
            myqueue.pop();
        }
    
        // Get the top element.
        int top() {
            return myqueue.front();
        }
    
        // Return whether the stack is empty.
        bool empty() {
            return myqueue.empty();
        }
    };

Log in to reply
 

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