其他分享
首页 > 其他分享> > 232. Implement Queue using Stacks

232. Implement Queue using Stacks

作者:互联网

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/

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

标签:压入,int,outStack,element,Queue,using,Implement,inStack,empty
来源: https://www.cnblogs.com/bky-hbq/p/12686230.html