@@ -131,6 +131,101 @@ public:
131
131
132
132
Java:
133
133
134
+ 使用Stack(堆栈)同名方法:
135
+ ```java
136
+ class MyQueue {
137
+ // java中的 Stack 有设计上的缺陷,官方推荐使用 Deque(双端队列) 代替 Stack
138
+ Deque<Integer> stIn;
139
+ Deque<Integer> stOut;
140
+ /** Initialize your data structure here. */
141
+ public MyQueue() {
142
+ stIn = new ArrayDeque<>();
143
+ stOut = new ArrayDeque<>();
144
+ }
145
+
146
+ /** Push element x to the back of queue. */
147
+ public void push(int x) {
148
+ stIn.push(x);
149
+ }
150
+
151
+ /** Removes the element from in front of queue and returns that element. */
152
+ public int pop() {
153
+ // 只要 stOut 为空,那么就应该将 stIn 中所有的元素倒腾到 stOut 中
154
+ if (stOut.isEmpty()) {
155
+ while (!stIn.isEmpty()) {
156
+ stOut.push(stIn.pop());
157
+ }
158
+ }
159
+ // 再返回 stOut 中的元素
160
+ return stOut.pop();
161
+ }
162
+
163
+ /** Get the front element. */
164
+ public int peek() {
165
+ // 直接使用已有的pop函数
166
+ int res = this.pop();
167
+ // 因为pop函数弹出了元素res,所以再添加回去
168
+ stOut.push(res);
169
+ return res;
170
+ }
171
+
172
+ /** Returns whether the queue is empty. */
173
+ public boolean empty() {
174
+ // 当 stIn 栈为空时,说明没有元素可以倒腾到 stOut 栈了
175
+ // 并且 stOut 栈也为空时,说明没有以前从 stIn 中倒腾到的元素了
176
+ return stIn.isEmpty() && stOut.isEmpty();
177
+ }
178
+ }
179
+ ```
180
+
181
+ 个人习惯写法,使用Deque通用api:
182
+ ``` java
183
+ class MyQueue {
184
+ // java中的 Stack 有设计上的缺陷,官方推荐使用 Deque(双端队列) 代替 Stack
185
+ // Deque 中的 addFirst、removeFirst、peekFirst 等方法等效于 Stack(堆栈) 中的 push、pop、peek
186
+ Deque<Integer > stIn;
187
+ Deque<Integer > stOut;
188
+ /* * Initialize your data structure here. */
189
+ public MyQueue () {
190
+ stIn = new ArrayDeque<> ();
191
+ stOut = new ArrayDeque<> ();
192
+ }
193
+
194
+ /* * Push element x to the back of queue. */
195
+ public void push (int x ) {
196
+ stIn. addLast(x);
197
+ }
198
+
199
+ /* * Removes the element from in front of queue and returns that element. */
200
+ public int pop () {
201
+ // 只要 stOut 为空,那么就应该将 stIn 中所有的元素倒腾到 stOut 中
202
+ if (stOut. isEmpty()) {
203
+ while (! stIn. isEmpty()) {
204
+ stOut. addLast(stIn. pollLast());
205
+ }
206
+ }
207
+ // 再返回 stOut 中的元素
208
+ return stOut. pollLast();
209
+ }
210
+
211
+ /* * Get the front element. */
212
+ public int peek () {
213
+ // 直接使用已有的pop函数
214
+ int res = this . pop();
215
+ // 因为pop函数弹出了元素res,所以再添加回去
216
+ stOut. addLast(res);
217
+ return res;
218
+ }
219
+
220
+ /* * Returns whether the queue is empty. */
221
+ public boolean empty () {
222
+ // 当 stIn 栈为空时,说明没有元素可以倒腾到 stOut 栈了
223
+ // 并且 stOut 栈也为空时,说明没有以前从 stIn 中倒腾到的元素了
224
+ return stIn. isEmpty() && stOut. isEmpty();
225
+ }
226
+ }
227
+ ```
228
+
134
229
``` java
135
230
class MyQueue {
136
231
198
293
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
199
294
* B站视频:[ 代码随想录] ( https://space.bilibili.com/525438321 )
200
295
* 知识星球:[ 代码随想录] ( https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ )
201
- <div align =" center " ><img src =../pics/公众号.png width =450 alt= > </img ></div >
296
+ <div align =" center " ><img src =../pics/公众号.png width =450 alt= > </img ></div >
0 commit comments