Implement Queue by Two Stacks
来源:互联网 发布:g3开票软件 编辑:程序博客网 时间:2024/06/04 17:59
Implement Queue by Two Stacks
30:00
As the title described, you should only use two stacks to implement a queue's actions.
The queue should support push(element)
, pop()
andtop()
where pop is pop the first(a.k.a front) element in the queue.
Both pop and top methods should return the value of first element.
看代码实现
/** * Created by jason on 2016/3/3. */class Queue { private Stack<Integer> stack1; private Stack<Integer> stack2; public Queue() { //do something stack1 = new Stack<Integer>(); stack2 = new Stack<Integer>(); } public void stack1Tostack2() { while (!stack1.empty()) { stack2.push(stack1.peek()); stack1.pop(); } } public void push(int element) { stack1.push(element); } public int pop() { if (stack2.empty()) { this.stack1Tostack2(); } return stack2.pop(); } public int top() { if (stack2.empty()) { this.stack1Tostack2(); } return stack2.peek(); }}
0 0
- implement-queue-by-two-stacks
- Implement Queue by Two Stacks
- Implement Queue by Two Stacks
- Implement Queue by Two Stacks
- Implement Queue by Two Stacks
- implement a queue by using two stacks
- [刷题]Implement Queue by Two Stacks
- lintcode: Implement Queue by Two Stacks
- #40 Implement Queue by Two Stacks
- [Lintcode]Implement Queue by Two Stacks
- Implement Queue by Two Stacks 解题报告
- Lintcode 40:Implement Queue by Two Stacks
- implement a queue by using two stacks [No. 31]
- Lintcode40 Implement Queue by Two Stacks solution 题解
- 【LeetCode】 Implement Queue by Two Stacks用栈实现队列
- Implement Queue with Two Stacks
- Implement Queue with two Stacks Java
- 40.Implement Queue by Two Stacks-用栈实现队列(中等题)
- Xcode 7.2 免证书真机调试
- 第6章 让他人心悦诚服的心理策略
- WebSocket 原理
- 使用命名空间、头文件和实现文件
- linux面试之-多线程的用武之地
- Implement Queue by Two Stacks
- 《我们仨》读后感
- 第七章 争取他人积极帮助的心理策略
- ListView
- Java IO
- 读写分离 双机热备 分库分表 缓存服务器 正(反)向代理
- 解决uitableviewcell重绘出现重叠的现象
- VM redhat6.4下安装oracle 11g回顾
- linux面试之内存管理