Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbff6192

Browse files
committed
225-implement-stack-using-queues.md Added Python JavaScript solutions.
1 parent1595fb3 commitbff6192

File tree

3 files changed

+404
-1
lines changed

3 files changed

+404
-1
lines changed

‎README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ You can skip the more difficult problems and do them later.
4848
-[18. 4Sum](en/1-1000/18-4sum.md) was solved in_Python_.
4949

5050
#Stack
51-
-[232. Implement Queue using Stacks](en/1-1000/232-implement-queue-using-stacks.md)
51+
-[232. Implement Queue using Stacks](en/1-1000/232-implement-queue-using-stacks.md) was solved in_Python, JavaScript_.
52+
53+
#Queue
54+
-[225. Implement Stack using Queues](en/1-1000/225-implement-stack-using-queues.md) was solved in_Python, JavaScript_ and 2 ways.
5255

5356
#Dynamic Programming
5457
##Basics
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#225. Implement Stack using Queues - LeetCode Solution Best Practice
2+
LeetCode link:[225. Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues), difficulty:**Easy**
3+
4+
##Description of "225. Implement Stack using Queues"
5+
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (`push`,`top`,`pop`, and`empty`).
6+
7+
Implement the`MyStack` class:
8+
9+
-`void push(int x)` Pushes element`x` to the top of the stack.
10+
-`int pop()` Removes the element on the top of the stack and returns it.
11+
-`int top()` Returns the element on the top of the stack.
12+
-`boolean empty()` Returns`true` if the stack is empty,`false` otherwise.
13+
14+
**Notes**:
15+
16+
- You must use**only** standard operations of a queue, which means that only`push to back`,`peek/pop from front`,`size` and`is empty` operations are valid.
17+
- Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
18+
19+
20+
###[Example 1]
21+
**Input**:
22+
```javascript
23+
["MyStack","push","push","top","pop","empty"]
24+
[[], [1], [2], [], [], []]
25+
```
26+
27+
**Output**:
28+
```javascript
29+
[null,null,null,2,2,false]
30+
```
31+
32+
**Explanation**:
33+
```java
34+
MyStack myStack=newMyStack();
35+
myStack.push(1);
36+
myStack.push(2);
37+
myStack.top();// return 2
38+
myStack.pop();// return 2
39+
myStack.empty();// return False
40+
```
41+
42+
###[Constraints]
43+
-`1 <= x <= 9`
44+
- At most`100` calls will be made to`push`,`pop`,`top`, and`empty`.
45+
- All the calls to`pop` and`top` are valid.
46+
47+
###[Follow-up]
48+
Can you implement the stack using only one queue?
49+
50+
##Intuition
51+
1. Two queues are used, one for input and output, and the other for temporary storage.
52+
2. There are two options for using queues to simulate the functions of a stack:
53+
1. Option 1: Simplify the`push(x)` operation and complicate the`pop()` and`top()` operations. When`pop()` or`top()`, you need to find the last data with effort.
54+
2. Option 2: Simplify the`pop()` and`top()` operations and complicate the`push(x)` operation. When`push(x)`, you need to insert`x` into the front of the queue with effort.
55+
3. Advantages of Option 2: Less code; easier to understand logically because it sorts the data in the queue according to the`last in, first out` rule.
56+
4. This article mainly introduces`Option 2`, and the code of`Option 1` is attached in`Python` to facilitate readers to compare the two solutions.
57+
58+
##Complexity
59+
* Time:`push O(n)`,`pop O(1)`,`top O(1)`,`empty O(1)`.
60+
* Space:`O(n)`.
61+
62+
##JavaScript
63+
```javascript
64+
varMyStack=function () {
65+
this.queue= []
66+
this.queueTemp= []
67+
};
68+
69+
MyStack.prototype.push=function (x) {
70+
while (this.queue.length>0) {
71+
this.queueTemp.push(
72+
this.queue.shift()
73+
)
74+
}
75+
76+
this.queue.push(x)
77+
78+
while (this.queueTemp.length>0) {
79+
this.queue.push(
80+
this.queueTemp.shift()
81+
)
82+
}
83+
};
84+
85+
MyStack.prototype.pop=function () {
86+
returnthis.queue.shift()
87+
};
88+
89+
MyStack.prototype.top=function () {
90+
returnthis.queue[0]
91+
};
92+
93+
MyStack.prototype.empty=function () {
94+
returnthis.queue.length===0
95+
};
96+
```
97+
98+
##Python
99+
###方案一:不推荐,仅用于对比。
100+
```python
101+
classMyStack:
102+
def__init__(self):
103+
self.queue= deque()
104+
self.queue_temp= deque()
105+
106+
defpush(self,x:int) ->None:
107+
self.queue.append(x)
108+
109+
defpop(self) ->int:
110+
whilelen(self.queue)>1:
111+
self.queue_temp.append(
112+
self.queue.popleft()
113+
)
114+
115+
value=self.queue.popleft()
116+
117+
whileself.queue_temp:
118+
self.queue.append(
119+
self.queue_temp.popleft()
120+
)
121+
122+
return value
123+
124+
deftop(self) ->int:
125+
value=None
126+
127+
whileself.queue:
128+
value=self.queue.popleft()
129+
self.queue_temp.append(value)
130+
131+
whileself.queue_temp:
132+
self.queue.append(
133+
self.queue_temp.popleft()
134+
)
135+
136+
return value
137+
138+
defempty(self) ->bool:
139+
returnlen(self.queue)==0
140+
```
141+
142+
###方案二:推荐的方案。既短,又好理解。
143+
```python
144+
classMyStack:
145+
def__init__(self):
146+
self.queue= deque()
147+
self.queue_temp= deque()
148+
149+
defpush(self,x:int) ->None:
150+
whileself.queue:
151+
self.queue_temp.append(
152+
self.queue.popleft()
153+
)
154+
155+
self.queue.append(x)
156+
157+
whileself.queue_temp:
158+
self.queue.append(
159+
self.queue_temp.popleft()
160+
)
161+
162+
defpop(self) ->int:
163+
returnself.queue.popleft()
164+
165+
deftop(self) ->int:
166+
returnself.queue[0]
167+
168+
defempty(self) ->bool:
169+
returnlen(self.queue)==0
170+
```
171+
172+
##Java
173+
```java
174+
// Welcome to create a PR to complete the code of this language, thanks!
175+
```
176+
177+
##C++
178+
```cpp
179+
// Welcome to create a PR to complete the code of this language, thanks!
180+
```
181+
182+
##C#
183+
```c#
184+
// Welcome to create a PR to complete the code of this language, thanks!
185+
```
186+
187+
##Go
188+
```go
189+
// Welcome to create a PR to complete the code of this language, thanks!
190+
```
191+
192+
##Ruby
193+
```ruby
194+
# Welcome to create a PR to complete the code of this language, thanks!
195+
```
196+
197+
##C, Kotlin, Swift, Rust or other languages
198+
```
199+
// Welcome to create a PR to complete the code of this language, thanks!
200+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp