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 ...
随机推荐
-
EasyUI tab常用
获取选中的tab的title var tab = $('#tab_Employee').tabs('getSelected'); var t=tab.panel('options').title; t ...
-
Inno Setup设定只运行一个安装包
原文 http://zwkufo.blog.163.com/blog/static/25882512010292526944/?suggestedreading&wumii 在安装包中,经常会 ...
-
golang 学习路径
目录 一 了解 go 二 入门教程 三 安装运行环境 & IDE(goland) 四 gotour 五 简易源码解析 六 开始写代码 七 学习框架 八 惯用法 九 调优 一 了解 go 谷歌一 ...
-
SQL之NULL值的几种处理方式
1.创建测试表: drop table if exists tab_null_operator; create table tab_null_operator as select 1 as id,'c ...
-
Unix/Linux环境C编程新手教程(21) 各个系统HelloWorld跑起来效果怎样?
版权声明:本文为博主尹成联系QQ77025077,微信18510341407原创文章,欢迎转载侵权不究. https://blog.csdn.net/yincheng01/article/detail ...
-
oradebug 10046
一.对当前的session使用oradebug命令: SQL> conn / as sysdba Connected. SQL> oradebug setmypid Statement p ...
-
Unity3D中声音播放
Unity3D 播放声音需要使用 Audio Source 组件,并且需要 Audio Listener 组件配合,不然无法听到声音.Main Camera 会默认有 Audio Lisetener. ...
-
db2 MON_GET_PKG_CACHE_STMT 表函数 抓取分析SQL
MON_GET_PKG_CACHE_STMT 表函数 还可以使用 MON_GET_PKG_CACHE_STMT 表函数来查询当前 PACKAGE CACHE 中 SQL 语句(包括动态 SQL 和静态 ...
-
PHP 验证IP的合法性
php验证IP的合法性! function get_ip(){ //判断服务器是否允许$_SERVER if(isset($_SERVER)){ if(isset($_SERVER[HTTP_X_FO ...
-
Python 小知识点(6)--静态方法、类方法、属性方法
(1)静态方法-->-@staticmethod装饰类中方法 只是名义上归类管理, 实际上在静态方法里访问不了类或实例中的任何属性 class Dog(object): def __init__ ...