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之frame动画详解
上一篇我们说了android中的tween动画,这一篇我们说说frame动画,frame动画主要是实现了一种类似于gif动画的效果,就是多张图按预先设定好的时间依次连续显示. 新建一个android项 ...
- 分分钟教你集成沉浸式侧滑关闭Activity
网上搜索侧滑关闭Activity,几乎没有系统状态栏跟随页面一起联动的,有明显的撕裂感,而这里则是状态栏跟随页面联动的,说来集成也是简单,等会你就知道了. 个人习惯,写博客前喜欢先截图 1.首先以项目 ...
- PHP中的strtotime()对于31日求上个月有问题
原文出处 <?php $date = "2012-07-31"; $date_unix = strtotime($date); $lastmonth = strtotime( ...
- bootstrap学习和使用的经验总结
第一肯定是下载 然后就是目录介绍,因为bootstrap是个轻量级的框架,目录不是很多,所以很容易理解,主要有用的就是三个文件,bootstrap.js,bootstrap.css,bootstrap ...
- 如何用js检测判断时间日期的间距
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Bootstrap-全局css样式之按钮
这里所说的按钮只是Bootstrap设计的能使标签或元素呈现按钮样式的属性,所以为 <a>.<button> 或 <input> 元素添加按钮类(button cl ...
- WisDom.Net 框架设计(八) 持久层
WisDom.Net ---持久层 1.什么是持久层 持久层负责最基础的功能支撑,为项目提供一个高层,统一,和并发的数据持久机制,提供了比如建立数据库连接,关闭数据库连接,执行sql语 ...
- Js 命名空间注册方法
MyApp = { namespace: function () { var a = arguments, o = null, i, j, d, rt; for (i = 0; i < a.le ...
- MSSQL生成整个数据库的SQL脚本的工具 scptxfr.exe
scptxfr.exe的路径要正确declare @cMd varchar(1000)set @cmd = 'master.dbo.xp_cmdshell ' + '''c:\"Micros ...
- struts2 标签的使用之一 s:if(遍历中s:if如何用等)
http://blog.csdn.net/chinajust/article/details/3922718