说明:问题描述来源于leetcode
一、问题描述:
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (68.96%) | 614 | - |
bloomberg
| microsoft
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push
、pop
、peek
、empty
):
实现 MyQueue
类:
void push(int x)
将元素 x 推到队列的末尾int pop()
从队列的开头移除并返回元素int peek()
返回队列开头的元素boolean empty()
如果队列为空,返回true
;否则,返回false
说明:
- 你 只能 使用标准的栈操作 —— 也就是只有
push to top
,peek/pop from top
,size
, 和is empty
操作是合法的。 - 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
提示:
1 <= x <= 9
- 最多调用
100
次push
、pop
、peek
和empty
- 假设所有操作都是有效的 (例如,一个空的队列不会调用
pop
或者peek
操作)
进阶:
- 你能否实现每个操作均摊时间复杂度为
O(1)
的队列?换句话说,执行n
个操作的总时间复杂度为O(n)
,即使其中一个操作可能花费较长时间。
前言
xin麒觉得可以用链表来实现一下如何构建队列。之前为了准备一下一个小比赛,就草草刷了一下题目,但是为了一个小比赛而草草刷题很容易没有自己的深度思考,于是xin麒重新写了这个题解。如果再够狠一点的应该可以自己通过实现一个ListNode来构建,当然啦,如果有时间而且有兴趣的情况下xin麒会去实现一下的。
二、题解1:通过链表来构造队列
1、前提引入:
定义成员变量list为List类型;
2、实现:
- 1、构造方法:首先是对list的初始化
分析:因为思路是以链表作为容器,而链表并不是凭空产生的,因此在构造方法对容器的初始化是合理正确的
- 2、void push:
直接使用List的add方法将元素添加到队列后面 - 3、int pop:
(1)分析:因为pop的作用是–>从队列的开头移除并返回元素
(2)①因此可以使用List的2、remove(int index)方法,直接传入参数0
将首元素在List中移除并返回该元素;
②在remove的时候,一定不要忘记可以remove的肯定是非空的List才可以,
因此要先判断是否为空,是则remove;否则直接返回0; - 4、int peek方法:
(1)分析:返回队列开头的元素
(2)理想到List的get(int index)方法,就知道了,因此
对于非空链表直接传入参数0返回索引为0的元素;
对于空链表则直接返回0。 - 5、boolean empty():
(1)分析:如果队列为空,返回 true ;否则,返回 false
(2)直接想到三目运算以及链表的size()方法,返回list.size() == 0
运算的结果即可
class MyQueue {
List<Integer> list;
public MyQueue() {
list = new LinkedList<>();
}
/**push
* 将元素 x 推到队列的末尾
* @param x
*/
public void push(int x) {
list.add(x);
}
/**pop
* 从队列的开头移除并返回元素
* @return
*/
public int pop() {
if (list.size() != 0){
return list.remove(0);
}
return 0;
}
/**peek
* 返回队列开头的元素
* @return
*/
public int peek() {
if (list.size() != 0){
return list.get(0);
}
return 0;
}
/**empty
* 如果队列为空,返回 true ;否则,返回 false
* @return
*/
public boolean empty() {
return list.size() == 0;
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
运行:
三、题解2:将两个堆栈作为容器,整合为一个队列般的系统
xin麒的思考过程:
如何将两个堆栈组装成为队列呢?首先从比较一下队列和堆栈的异同---->
-
共同点:队列和堆栈的push方法都是将元素压进容器
-
不同点:队列的pop方法是将队列的首元素移除并返回元素
堆栈的pop方法是将堆栈的末元素移除并返回元素
- peek方法可以类比pop方法
于是搞定队列的pop方法则为重点:
-
如果将一个堆栈的所有元素反转过来不就可以通过堆栈的pop方法提取首元素了吗?
-
因此可以将一个堆栈里的元素倒入另一个堆栈里面,通过另一个堆栈的pop方法来提取首元素
实现:
前提引入:
- 在类中定义两个成员变量
stackIn
和stackOut
的编译类型为Stack
细节:
- 构造方法:完成两个堆栈的初始化
- void push(int x):直接往
stackIn
里压进元素即可 - int pop():因为中心思想是将第一个堆栈的所有元素倒入到第二个堆栈里面,因此
- 如果之前倒入的元素没有被移除,那么可以直接将所要提取的元素pop出来
- 如果之前倒入的元素已经被移除了,那么就将第一个堆栈里的所有元素倒进第二个堆栈容器里面,前提是两第一个容器非空,最后return第二个堆栈的pop出来的元素即可
- 如果第一个容器为空,那么返回0即可。
- int peek():直接类比pop的处理即可
- boolean empty():
empty
方法比较好办,直接判断两个容器是否均为空即可
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
/**push
* 将元素 x 推到队列的末尾
* @param x
*/
public void push(int x) {
stackIn.push(x);
}
/**pop
* 从队列的开头移除并返回元素
* @return
*/
public int pop() {
if (!stackOut.isEmpty()){
return stackOut.pop();
}
if (!stackIn.isEmpty()){
while (!stackIn.isEmpty()){
stackOut.push(stackIn.pop());
}
return stackOut.pop();
}
return 0;
}
/**peek
* 返回队列开头的元素
* @return
*/
public int peek() {
if (!stackOut.isEmpty()){
return stackOut.peek();
}
if (!stackIn.isEmpty()){
while (!stackIn.isEmpty()){
stackOut.push(stackIn.pop());
}
return stackOut.peek();
}
return 0;
}
/**empty
* 如果队列为空,返回 true ;否则,返回 false
* @return
*/
public boolean empty() {
return stackOut.isEmpty() && stackIn.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
运行:
简化:
xin麒也看过其他的题解发现这个题解的代码可以简化,于是xin麒简化了一下。但是xin麒觉得困惑的是下面的简化出来的代码虽然可以在leetcode提交成功,但是如果是容器为空,那么returnpop方法(或peek)执行的结果
岂不是会报错了吗?xin麒想:可能是leetcode这个平台搞的鬼吧,xin麒的分析应该没错,因为xin麒有在自己IDEA里测试了空栈调用pop和peek的运行结果确实会抛EmptyStackException
异常。
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
xinDo();
return stackOut.pop();
}
public int peek() {
xinDo();
return stackOut.peek();
}
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
public void xinDo(){
if (!stackOut.isEmpty()){
return ;
}
if (!stackIn.isEmpty()){
while (!stackIn.isEmpty()){
stackOut.push(stackIn.pop());
}
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/