用两个栈实现队列(剑指Offer)
来源:互联网 发布:linux mint中文输入法 编辑:程序博客网 时间:2024/06/10 18:48
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
先知道一个最基本的:队列先进先出,栈先进后出
解答:
import java.util.Stack;public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { if(stack1.empty()&&stack2.empty()) throw new RuntimeException("Queue is empty!"); if(stack2.empty()){ while(!stack1.empty()) stack2.push(stack1.pop()); } return stack2.pop(); }}
阅读全文
0 0
- [剑指offer]用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- 剑指offer 用两个栈实现队列
- 剑指offer 用两个栈实现队列
- [剑指offer]用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指offer|用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer----用两个栈实现队列
- 约瑟夫环
- Android Tab类型主界面总结
- python 数据结构二 之 字符串
- java api操作hbase
- 用GDB调试程序
- 用两个栈实现队列(剑指Offer)
- codeforces 817D
- 2017暑假集训
- windows IOCP完成端口原理详解
- 2.0-Spark完全分布式集群安装
- 搭建WordPress
- 备份map类型
- 数据结构 斐波那契查找
- R语言svm支持向量机分类与参数优化