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 ...
随机推荐
- Palindrome Linked List 解答
Question Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O ...
- Android图形合成和显示系统---基于高通MSM8k MDP4平台
介绍了Android SurfaceFlinger层次以下的图形合成和显示系统,主要基于高通MSM8k MDP4x平台. 做为Android Display专题.SurfaceFlinger的详细介绍 ...
- POJ 3046 Ant Counting DP
大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...
- 使用ant自动编译、打包生成apk文件
上次使用命令行生成apk文件<Android 命令行编译.打包生成apk文件>,学习命令行生成的目的是为了编写ant打下基础. 一. ant环境 下载ant包,配置环境变量 二.ant编译 ...
- CSS选择符 新的认识
以前写选择符,很少使用">"符号,一般都是使用" "空格来代替.我觉得他们都是一样的功能,另一方面是为了方便.毕竟不用按住shift. 例如: .demo ...
- 企业qq代码,工作中用到的
<div id="xixi" onmouseover="toBig()" style="top: 120px; left: 0; positio ...
- 基于canvas图像处理的图片展示demo
图片展示网页往往色彩繁杂,当一个网页上有多张图片的时候用户的注意力就很不容易集中,而且会造成网站整个色调风格的不可把控. 能不能把所有的预览图变成灰度图片,等用户激活某张图片的时候再上色呢? 以前,唯 ...
- textView富文本点击事件
NSDictionary * attDic = [NSDictionary dictionaryWithObjectsAndKeys:RGBCOLOR(31, 132, 204),NSForegrou ...
- C#线程池ThreadPool的理解
在多线程编程中,线程的创建和销毁是非常消耗系统资源的,因此,C#引入了池的概念,类似的还有数据库连接池,这样,维护一个池,池内维护的一些线程,需要的时候从池中取出来,不需要的时候放回去,这样就避免了重 ...
- struts2 404处理
目前在做一个网络应用程序,struts2 + spring + hibernate,服务器是tomcat.希望用户在IE地址栏乱敲的时候,所敲入的所有未定义的URL都能被程序捕捉到,然后转到一个自制的 ...