码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 232. Implement Queue using Stacks

时间:2018-07-14 16:42:32      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:code   ted   pre   obj   hat   instant   color   returns   span   

class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        std::stack<int> aux;
        while(!_data.empty())
        {
            aux.push(_data.top());
            _data.pop();
        }
        aux.push(x);
        while(!aux.empty())
        {
            _data.push(aux.top());
            aux.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int x  = _data.top();
        _data.pop();
        return x;
    }
    
    /** Get the front element. */
    int peek() {
        return _data.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return _data.empty();
    }
private:
    std::stack<int> _data;
    
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */

 

Leetcode 232. Implement Queue using Stacks

标签:code   ted   pre   obj   hat   instant   color   returns   span   

原文地址:https://www.cnblogs.com/randyniu/p/9309462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!