mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
修改0232.用栈实现队列 Java版本
修改0232.用栈实现队列 Java版本
This commit is contained in:
@ -131,6 +131,101 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
使用Stack(堆栈)同名方法:
|
||||||
|
```java
|
||||||
|
class MyQueue {
|
||||||
|
// java中的 Stack 有设计上的缺陷,官方推荐使用 Deque(双端队列) 代替 Stack
|
||||||
|
Deque<Integer> stIn;
|
||||||
|
Deque<Integer> stOut;
|
||||||
|
/** Initialize your data structure here. */
|
||||||
|
public MyQueue() {
|
||||||
|
stIn = new ArrayDeque<>();
|
||||||
|
stOut = new ArrayDeque<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Push element x to the back of queue. */
|
||||||
|
public void push(int x) {
|
||||||
|
stIn.push(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Removes the element from in front of queue and returns that element. */
|
||||||
|
public int pop() {
|
||||||
|
// 只要 stOut 为空,那么就应该将 stIn 中所有的元素倒腾到 stOut 中
|
||||||
|
if (stOut.isEmpty()) {
|
||||||
|
while (!stIn.isEmpty()) {
|
||||||
|
stOut.push(stIn.pop());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 再返回 stOut 中的元素
|
||||||
|
return stOut.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the front element. */
|
||||||
|
public int peek() {
|
||||||
|
// 直接使用已有的pop函数
|
||||||
|
int res = this.pop();
|
||||||
|
// 因为pop函数弹出了元素res,所以再添加回去
|
||||||
|
stOut.push(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns whether the queue is empty. */
|
||||||
|
public boolean empty() {
|
||||||
|
// 当 stIn 栈为空时,说明没有元素可以倒腾到 stOut 栈了
|
||||||
|
// 并且 stOut 栈也为空时,说明没有以前从 stIn 中倒腾到的元素了
|
||||||
|
return stIn.isEmpty() && stOut.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
个人习惯写法,使用Deque通用api:
|
||||||
|
```java
|
||||||
|
class MyQueue {
|
||||||
|
// java中的 Stack 有设计上的缺陷,官方推荐使用 Deque(双端队列) 代替 Stack
|
||||||
|
// Deque 中的 addFirst、removeFirst、peekFirst 等方法等效于 Stack(堆栈) 中的 push、pop、peek
|
||||||
|
Deque<Integer> stIn;
|
||||||
|
Deque<Integer> stOut;
|
||||||
|
/** Initialize your data structure here. */
|
||||||
|
public MyQueue() {
|
||||||
|
stIn = new ArrayDeque<>();
|
||||||
|
stOut = new ArrayDeque<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Push element x to the back of queue. */
|
||||||
|
public void push(int x) {
|
||||||
|
stIn.addLast(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Removes the element from in front of queue and returns that element. */
|
||||||
|
public int pop() {
|
||||||
|
// 只要 stOut 为空,那么就应该将 stIn 中所有的元素倒腾到 stOut 中
|
||||||
|
if (stOut.isEmpty()) {
|
||||||
|
while (!stIn.isEmpty()) {
|
||||||
|
stOut.addLast(stIn.pollLast());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 再返回 stOut 中的元素
|
||||||
|
return stOut.pollLast();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the front element. */
|
||||||
|
public int peek() {
|
||||||
|
// 直接使用已有的pop函数
|
||||||
|
int res = this.pop();
|
||||||
|
// 因为pop函数弹出了元素res,所以再添加回去
|
||||||
|
stOut.addLast(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns whether the queue is empty. */
|
||||||
|
public boolean empty() {
|
||||||
|
// 当 stIn 栈为空时,说明没有元素可以倒腾到 stOut 栈了
|
||||||
|
// 并且 stOut 栈也为空时,说明没有以前从 stIn 中倒腾到的元素了
|
||||||
|
return stIn.isEmpty() && stOut.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
class MyQueue {
|
class MyQueue {
|
||||||
|
|
||||||
@ -198,4 +293,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
Reference in New Issue
Block a user