Implement Queue using Stacks 解答
Question
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Notes:
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
Solution
enQueue(q, x)
1) Push x to stack1 (assuming size of stacks is unlimited). deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything from satck1 to stack2.
3) Pop the element from stack2 and return it.
Check each element i, i can only be pushed/ poped once at stack 1, and pushed/ poped once at stack 2. So add(), remove(), peek() still cost O(1) time.
public class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2; public Queue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
} public void push(int element) {
stack1.push(element);
} public int pop() {
if (stack1.isEmpty() && stack2.isEmpty())
return 0;
if (stack2.isEmpty()) {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
return stack2.pop();
} public int top() {
if (stack1.isEmpty() && stack2.isEmpty())
return 0;
if (stack2.isEmpty()) {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
return stack2.peek();
}
}
Implement Queue using Stacks 解答的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- c++ 13
一.向量 ... 10.size/resize/clear/capacity/reserve 1)向量的大小可增可减,使向量大小改变的函数包括:resize/push_back/pop_back/cl ...
- 发几个速度快可以用的google IP,谷歌IP(转)
google搜索引擎打不开时的解决办法,谷歌(google)的IP是多少? google IP镜像. 这里搜集了几个经过测试可用的IP,用来在不能域名访问google的时候进行访问,实时更新! 前面几 ...
- shell编程笔记(1)
shell编程: 编译器,解释器 编程语言:机器语言.汇编语言.高级语言 静态语言:编译型语言 强类型(变量) 事先转换成可执行格式 C.C++.JAVA.C# ...
- 修改UISearchBar placeholder textColor
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
- 给一个int型整数,如何将这个整数的奇偶位互换
题目: 假设一个8为整数是(10101100)b那么奇偶互换之后就是(01011100)b.假设机器是32位的 注意: 8位中最低位开始数,最低位是第0位,是偶数为,次低位时第1位,是偶数位. 做法: ...
- 【解决方法】EasyUI DataGrid不显示滚动条时,没有数据的问题
解决方法 于dataGrid例如,下面的代码被添加到的定义: JavaScript Code 1 2 3 4 5 6 7 8 9 10 onLoadSuccess : function (data ...
- The account is locked
SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...
- freemarker书写select组件错误摘要(七)
1.错误叙述性说明 六月 26, 2014 11:26:27 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...
- nodejs 批处理运行 app.js
1.直接执行run.bat文件 以下的内容为批处理文件run.bat中的内容,批处理命令中NODE_PATH为Node.js的安装路径. 使用express 生成的项目.app.js为 ...
- JQuery hover(over,out) 使用笔记
转载自:http://www.douban.com/note/202404884/ JQuery hover(over,out) 使用笔记 JavaScript 下.onmouseover() 和 o ...