leetcode 232. Implement Queue using Stacks

来源:互联网 发布:java的网络编程重要吗 编辑:程序博客网 时间:2024/06/05 05:11
class MyQueue {public:    /** Initialize your data structure here. */    stack<int> pstack;    stack<int> auxstack;    MyQueue() {    }        /** Push element x to the back of queue. */    void push(int x) {        pstack.push(x);            }        /** Removes the element from in front of queue and returns that element. */    int pop() {        int ans = peek();        auxstack.pop();        return ans;            }        /** Get the front element. */    int peek() {        if(auxstack.empty()){            while(!pstack.empty()){                auxstack.push(pstack.top());                pstack.pop();            }        }        return auxstack.top();            }        /** Returns whether the queue is empty. */    bool empty() {        return pstack.empty()&&auxstack.empty();            }};

0 0
原创粉丝点击