题解 | #用两个栈实现队列#
用两个栈实现队列
https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6
//定义两个栈,保证一个只进,一个只出 class Solution { public: void push(int node) {{ stack1.push(node); } } int pop() { if (stack2.empty()&&stack1.empty()) { return -1; } else if (stack2.empty()) { while (!stack1.empty()) { int num=stack1.top(); stack2.push(num); stack1.pop(); } int re =stack2.top(); stack2.pop(); return re; } else { int re =stack2.top(); stack2.pop(); return re; } } private: stack<int> stack1; stack<int> stack2; };