首页 > 学院 > 开发设计 > 正文

剑指offer-用两个栈实现队列

2019-11-08 02:45:10
字体:
来源:转载
供稿:网友

问题

题目:[用两个栈实现队列]

思路

入队好办,出队的时候把一个栈的元素倒入另外一个栈即可。

代码

class Solution{public: void push(int node) { stack1.push(node); } int pop() { helper(stack1,stack2); int front = stack2.top(); stack2.pop(); helper(stack2, stack1); return front; }PRivate: void helper( stack<int>& s1, stack<int>& s2 ){ while(!s1.empty()){ int top = s1.top(); s1.pop(); s2.push(top); } }private: stack<int> stack1; stack<int> stack2;};
上一篇:cigarettes

下一篇:LintCode 17 子集

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表