题解 | #用两个栈实现队列#

用两个栈实现队列

http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }
    int pop() {
        //if(stack1.empty()) return 0;
        copy(stack2, stack1);
        int res = stack2.top();
        stack2.pop();
        copy(stack1, stack2);
        return res;
    }
    //将b的导入a中
    void copy(stack<int> &a, stack<int> &b){
        //if(a.empty() || b.empty()) return;
        while(b.size()){
            a.push(b.top());
            b.pop();
        }
    }
private:
    stack<int> stack1;
    stack<int> stack2;
};
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务