Implement Stack using Queues 解答
Question
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Solution
Key to the solution is to use two queues. Queue is strictly First In, First Out.
Two Methods
Method 1: making push operation costly
push(s, x) // x is the element to be pushed and s is stack
1) Enqueue x to q2
2) One by one dequeue everything from q1 and enqueue to q2.
3) Swap the names of q1 and q2
// Swapping of names is done to avoid one more movement of all elements
// from q2 to q1. pop(s)
1) Dequeue an item from q1 and return it.
Method 2: making pop operation costly
push(s, x)
1) Enqueue x to q1 (assuming size of q1 is unlimited). pop(s)
1) One by one dequeue everything except the last element from q1 and enqueue to q2.
2) Dequeue the last item of q1, the dequeued item is result, store it.
3) Swap the names of q1 and q2
4) Return the item stored in step 2.
// Swapping of names is done to avoid one more movement of all elements
// from q2 to q1.
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2; public MyStack(){
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
} // Push element x onto stack.
public void push(int x) {
queue1.add(x);
} // Removes the element on top of the stack.
public void pop() {
if (queue1.peek() == null)
return;
int length = queue1.size();
queue2 = new LinkedList<Integer>();
while (length > 1) {
queue2.add(queue1.remove());
length--;
}
queue1 = queue2;
} // Get the top element.
public int top() {
if (queue1.peek() == null)
return -1;
queue2 = new LinkedList<Integer>();
int length = queue1.size();
int lastElement = 0;
while (length > 0) {
lastElement = queue1.remove();
queue2.add(lastElement);
length--;
}
queue1 = queue2;
return lastElement;
} // Return whether the stack is empty.
public boolean empty() {
if (queue1.peek() == null)
return true;
return false;
}
}
Implement Stack using Queues 解答的更多相关文章
- 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- Java内存回收(垃圾回收)机制总结
一.背景: Java程序员编写程序时,对于新建的对象,当不再需要此对象时,不必去释放这个对象所占用的空间,这个工作是由Java虚拟机自己完成的 ,即内存回收或垃圾回收. 二.如何知道一个对象所占用的空 ...
- Hive 2、Hive 的安装配置(本地MySql模式)
一.前提条件 安装了Zookeeper.Hadoop HDFS HA 安装方法: http://www.cnblogs.com/raphael5200/p/5154325.html 二.安装Mysq ...
- hdu 5335 Walk Out(bfs+寻找路径)
Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...
- PLSql连接远程Oracle方法
- Mysql 复制表结构 及其表的内容
顺便转一下Mysql复制表结构.表数据的方法: 1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们 ...
- bootstrap 常用类名
一. 常用类1.container居中的内容展示2.row 行内容显示3.col 列内容显示, 列必须在row 中xs 宽度小于768 ,sm宽度小于990 大于768 ,md 宽度大于990,小于 ...
- mvc分页生成静态页,mvc生成静态页
http://blog.csdn.net/xxj_jing/article/details/7899125 分页生成静态页 http://www.cnblogs.com/luanyilin/archi ...
- Android界面优化方法
我们在推出一款APP之后,中间出现了一些体验上的问题,一个明显的是界面卡顿,针对此问题我们采取了如下的一些措施,起到了一些效果. 1.优化界面层次 针对可以合并的界面层次进行合并,减少界面的渲染,这个 ...
- ORA-01152错误解决方法(转)
具体步骤如下: startup force; alter system set "_allow_resetlogs_corruption"=true scope=spfile; r ...
- COMException 依赖服务或组无法启动(0x8007042C)处理办法
问题分析:这个问题主要原因是由于服务列表中的windows management instrumentation这个服务无法启动 问题解决办法: 点击屏幕左下角:开始-运行-输入regedit 打开注 ...