牛客-用兩個棧實現隊列

牛客-用兩個棧實現隊列

#include <stdio.h>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <string.h>
#include <string>
#include <cstring>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        if (!stack2.empty()) {
            int top = stack2.top();
            stack2.pop();
            return top;
        } else {
            while (!stack1.empty()) {
                int top = stack1.top();
                stack2.push(top);
                stack1.pop();
            }
            int top = stack2.top();
            stack2.pop();
            return top;
        }

    }

private:
    stack<int> stack1;
    stack<int> stack2;
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章