剑指offer-5.用两个栈实现队列
来源:互联网 发布:手机淘宝兼职是真的吗 编辑:程序博客网 时间:2024/05/21 14:49
题目:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路:用栈1负责入队(当然,如果你想栈2负责入队也是可以的),栈2负责出队操作,入队操作无需考虑栈里是否为空(这里不考虑上溢情况),而出队操作必须考虑栈是否为空,因为栈为空,就必须从栈1获取元素,付给栈2,然后栈2才能执行出队操作。
class Solution{public: void push(int node) { stack1.push(node); //栈1负责入队工作 } int pop() { if (stack2.empty()) { while (!stack1.empty()) { stack2.push(stack1.top()); stack1.pop(); } } int result = stack2.top(); stack2.pop(); return result; }private: stack<int> stack1; stack<int> stack2;};
0 0
- [剑指offer]用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- 剑指offer 用两个栈实现队列
- 剑指offer 用两个栈实现队列
- [剑指offer]用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指offer|用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer----用两个栈实现队列
- STL类指针容器container<class*> 元素clear 注意事项
- Python IO流(Input&Output)
- Bootstrap进度条与AJAX后端数据传递结合使用
- 反向代理【nginx】
- Linux中文件查看命令cat/more/less
- 剑指offer-5.用两个栈实现队列
- Java图形界面Swing之窗体居中设置
- 团体程序设计天梯赛-练习集 -- L3-016. 二叉搜索树的结构(模拟)
- spring hibernate事务的流程
- 基于路由机制设计的app架构思路
- 装饰模式
- android 创建版本为1的数据库
- 自定义 matplotlib 设置
- 关于线程基础