lc面试准备:Implement Stack using Queues
1 题目
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, and is 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).
接口: 实现4个方法
2 思路
用2个queue来实现,java推荐用LinkedList作为Queue.
Version A: The stack should be efficient when pushing an item.
Version B: The stack should be efficient when popping an item.
Version A: push O(1); pop O(n)
push:
- enqueue in queue1
pop:
- while size of queue1 is bigger than 1, pipe dequeued items from queue1 into queue2
- dequeue and return the last item of queue1, then switch the names of queue1 and queue2
Version B: push O(n); pop O(1)
push:
- enqueue in queue2
- enqueue all items of queue1 in queue2, then switch the names of queue1 and queue2
pop:
- deqeue from queue1
3 代码
Vesion A
class MyStackVersionA {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
q1.add(x);
}
// Removes the element on top of the stack.
public void pop() {
top();
q1.poll();
Queue<Integer> tmp = q1;
q1 = q2;
q2 = tmp;
}
// Get the top element.
public int top() {
int size = q1.size();
if (size > 1) {
int count = size - 1;
for (int i = 0; i < count; i++) {
q2.add(q1.poll());
}
}
return q1.peek();
}
// Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}
Vesion B
class MyStackVersionB {
Queue<Integer> q1 = new LinkedList<Integer>();
Queue<Integer> q2 = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
q2.add(x);
int size = q1.size();
for(int i = 0; i < size; i++) {
q2.add(q1.poll());
}
Queue<Integer> tmp = q1;
q1 = q2;
q2 = tmp;
}
// Removes the element on top of the stack.
public void pop() {
q1.poll();
}
// Get the top element.
public int top() {
return q1.peek();
}
// Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty() && q2.isEmpty();
}
}
4 总结
- 用两个
queue实现stack, pop 和 push的效率的选择。 - 由于题目假设
pop和peek都不会在队列为空的时候执行,避免了Null Pointer Exception. - stack 和 queue的相互实现,很好的考察基本功。
5 参考
lc面试准备: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) -- ...
- [LC] 225. 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 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 ...
随机推荐
- android开发之手势识别
由于精确度等原因,手势识别在android中用的并不多,不过这并不妨碍我们来玩玩这个神奇的玩意. 在android中要使用手势,先得建立手势库,建立手势库非常简单,新建一个android sample ...
- ZeroClipboard.swf
ZeroClipboard.config({ moviePath: "source/ZeroClipboard.swf", hove ...
- 转-C# 操作 Excel 常见问题收集和整理
经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个最基本的问题. 做开发这几年来,陆陆续续也接触过这样的需求,但因为 ...
- css制作导航栏的上下三角
1)先完成一个导航条 <style type="text/css"> .nav-ul{ list-style: none; } .nav-ul li{ width: 1 ...
- web前端:html
一.理解表单的作用 1.web 应用程序不仅仅是给用户显示数据,还应该给用户提供一个可以输入数据的图形用户界面.表单的主要作用在于在网页上提供一个图形用户界面,已采集和提交用户输入的数据. 2.htm ...
- CSS权威指南-第三版--读书笔记
第一章:CSS和文档 html是结构化语言,css是样式语言,html主要用来被强大的搜索引擎更好的索引,更好的让一个盲人通过语音浏览器来了解我们的网页,这也就是为什么说html是结构话语言,因为这是 ...
- javaBean登录注册
package javabean; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Res ...
- Spring Framework jar官方直接下载路径
SPRING官方网站改版后,建议都是通过 Maven和Gradle下载,对不使用Maven和Gradle开发项目的,下载就非常麻烦,下给出Spring Framework jar官方直接下载路径: h ...
- Javascript 汉字转拼音
调用方式: var pinyin = convert("欢迎光临"); alert(pinyin); 新建JS文件:PYConvert.js,内容如下: var PinYin = ...
- [XML] Resource帮助类
点击下载 Resources.rar /// <summary> /// 类说明:Resources /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[u ...