题解:#二个栈实现一个队列#
class Solution { public: void push(int node) { stack1.push(node); } int pop() { // int data= stack1.top(); // stack1.pop(); // stack2.push(data); while (!stack1.empty()) { int data= stack1.top(); stack1.pop(); stack2.push(data); } //这样做是因为输入案列需要一个一个pop所以剩下的都需要返回到stack中 int res = stack2.top(); stack2.pop(); while (!stack2.empty()) { stack1.push(stack2.top()); stack2.pop(); } return res; } private: stack<int> stack1; stack<int> stack2; };#剑指offer#
剑指offer刷题 文章被收录于专栏
坚持!努力!学习