题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路:
1、一个栈用来做push
2、另一个栈用来做pop
3、将push操作的栈的元素放入另一个栈中,实现先进先出
class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }
    int pop() {
        if(stack2.empty())
        {
            while(!stack1.empty())
            {
                int num = stack1.top();
                stack2.push(num);
                stack1.pop();
            }
        }
        int res = stack2.top();
        stack2.pop();
        return res;
            
    }

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

 

相关文章:

  • 2021-11-10
  • 2022-12-23
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
猜你喜欢
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-12-11
  • 2021-06-20
  • 2022-01-09
相关资源
相似解决方案