Skip to content

Commit ba02f7f

Browse files
committed
225-implement-stack-using-queues.md Reworded.
1 parent 8a381aa commit ba02f7f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

en/1-1000/225-implement-stack-using-queues.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ myStack.empty(); // return False
4444
- At most `100` calls will be made to `push`, `pop`, `top`, and `empty`.
4545
- All the calls to `pop` and `top` are valid.
4646

47-
### [Follow-up]
48-
Can you implement the stack using only one queue?
49-
5047
## Intuition
5148
1. Two queues are used, one for input and output, and the other for temporary storage.
5249
2. There are two options for using queues to simulate the functions of a stack:
@@ -60,11 +57,15 @@ Can you implement the stack using only one queue?
6057
* Space: `O(n)`.
6158

6259
## Follow-up
60+
Can you implement the stack using only one queue?
61+
62+
### Follow-up Intuition
6363
- You can use only one queue to make it. The only change is in the `push` method. Just find a way to insert `x` to the front of the queue without using another `queue_temp`.
64-
- When implementing the `push` method, first `queue.push(x)`, then execute `queue.length - 1` times `queue.push(queue.pop())`. The complete code is attached in `JavaScript` section.
64+
- When implementing the `push` method, first `queue.push(x)`, so that `x` is inserted at the tail (back) of the queue, but we need to put `x` at the head (front) of the queue.
65+
- Execute `queue.length - 1` times `queue.push(queue.pop())` to move all the data before `x` to the back of `x`. The complete code is attached in the `JavaScript` section.
6566

6667
## JavaScript
67-
### Solution for option 2
68+
### "Option 2" solution
6869
```javascript
6970
var MyStack = function () {
7071
this.queue = []
@@ -100,7 +101,7 @@ MyStack.prototype.empty = function () {
100101
};
101102
```
102103

103-
### Follow-up solution: use only one queue
104+
### "Follow-up" solution: use only one queue
104105
```javascript
105106
var MyStack = function () {
106107
this.queue = []
@@ -129,7 +130,7 @@ MyStack.prototype.empty = function () {
129130
```
130131

131132
## Python
132-
### Solution for option 1: Not recommended, for comparison only.
133+
### "Option 1" solution: Not recommended, for comparison only.
133134
```python
134135
class MyStack:
135136
def __init__(self):
@@ -172,7 +173,7 @@ class MyStack:
172173
return not self.queue
173174
```
174175

175-
### Solution for option 2: It is short and easy to understand (recommended).
176+
### "Option 2" solution: It is short and easy to understand (recommended).
176177
```python
177178
class MyStack:
178179
def __init__(self):

zh/1-1000/225-implement-stack-using-queues.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ myStack.empty(); // 返回 False
4343
- 最多调用 `100``push``pop``top``empty`
4444
- 每次调用 `pop``top` 都保证栈不为空
4545

46-
### [进阶]
47-
你能否仅用一个队列来实现栈?
48-
49-
# 中文题解
5046
## 思路
5147
1. 使用的两个队列,一个队列用于输入和输出,另一个队列用于临时存储。
5248
2. 用队列模拟栈的功能,有两种方案可供选择:
@@ -60,8 +56,13 @@ myStack.empty(); // 返回 False
6056
* 空间:`O(n)`
6157

6258
## 进阶
59+
你能否仅用一个队列来实现栈?
60+
61+
### 进阶思路
6362
- 可以只用一个队列实现栈。改动只在`push`方法。只需要想办法不借助另一个`queue_temp`,把`x`插入到队列的头部。
64-
- 在实现`push`方法时,先`queue.push(x)`,然后,执行`queue.length - 1``queue.push(queue.pop())`即可。完整代码附在`JavaScript`章节中。
63+
- 在实现`push`方法时,先`queue.push(x)`,这样,`x`就被插入到了队列尾部,但我们需要把`x`放到队列的头部。
64+
-`x`前面的所有数据依次出队列,再入队列,就可以了。
65+
- 执行`queue.length - 1``queue.push(queue.pop())`即可。完整代码附在`JavaScript`章节中。
6566

6667
## JavaScript
6768
### 方案二

0 commit comments

Comments
 (0)