标签:顺序 实现 public off code ack void offer scribe
1 class Solution 2 { 3 public: 4 void push(int node) { 5 while(!stack2.empty()) 6 { 7 stack1.push(stack2.top()); 8 stack2.pop(); 9 } 10 stack1.push(node); 11 } 12 13 int pop() { 14 while(!stack1.empty()) 15 { 16 stack2.push(stack1.top()); 17 stack1.pop(); 18 } 19 int node = stack2.top(); 20 stack2.pop(); 21 return node; 22 } 23 private: 24 stack<int> stack1; 25 stack<int> stack2; 26 };
思考:入栈1时,由于时逆序,那么当前进栈元素应该最后进栈,放在栈顶第一个元素的位置,这样转移到栈2时就会在栈底,即满足队列的FILO。
标签:顺序 实现 public off code ack void offer scribe
原文地址:https://www.cnblogs.com/Swetchine/p/11296942.html