C++ simple solution


  • 0
    A
      class Queue {
        stack<int> m_stack;
    public:
        // Push element x to the back of queue.
        void push(int x) {
            stack<int> temp;
            while (!m_stack.empty()){
                temp.push(m_stack.top());
                m_stack.pop();
            }
            
            m_stack.push(x);
            while (!temp.empty()) {
                m_stack.push(temp.top());
                temp.pop();
            }
        }
    
        // Removes the element from in front of queue.
        void pop(void) {
            return m_stack.pop();
        }
    
        // Get the front element.
        int peek(void) {
            return m_stack.top();
        }
    
        // Return whether the queue is empty.
        bool empty(void) {
            return m_stack.empty();
        }
    };

Log in to reply
 

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