@@ -130,43 +130,42 @@ Java:
130
130
```java
131
131
class MyQueue {
132
132
133
- Stack<Integer> stack1 ;
134
- Stack<Integer> stack2 ;
133
+ Stack<Integer> stackIn ;
134
+ Stack<Integer> stackOut ;
135
135
136
136
/** Initialize your data structure here. */
137
137
public MyQueue() {
138
- stack1 = new Stack<>(); // 负责进栈
139
- stack2 = new Stack<>(); // 负责出栈
138
+ stackIn = new Stack<>(); // 负责进栈
139
+ stackOut = new Stack<>(); // 负责出栈
140
140
}
141
141
142
142
/** Push element x to the back of queue. */
143
143
public void push(int x) {
144
- stack1 .push(x);
144
+ stackIn .push(x);
145
145
}
146
146
147
147
/** Removes the element from in front of queue and returns that element. */
148
148
public int pop() {
149
- dumpStack1 ();
150
- return stack2 .pop();
149
+ dumpstackIn ();
150
+ return stackOut .pop();
151
151
}
152
152
153
153
/** Get the front element. */
154
154
public int peek() {
155
- dumpStack1 ();
156
- return stack2 .peek();
155
+ dumpstackIn ();
156
+ return stackOut .peek();
157
157
}
158
158
159
159
/** Returns whether the queue is empty. */
160
160
public boolean empty() {
161
- return stack1 .isEmpty() && stack2 .isEmpty();
161
+ return stackIn .isEmpty() && stackOut .isEmpty();
162
162
}
163
163
164
- // 如果stack2为空,那么将stack1中的元素全部放到stack2中
165
- private void dumpStack1(){
166
- if (stack2.isEmpty()){
167
- while (!stack1.isEmpty()){
168
- stack2.push(stack1.pop());
169
- }
164
+ // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
165
+ private void dumpstackIn(){
166
+ if (!stackOut.isEmpty()) return;
167
+ while (!stackIn.isEmpty()){
168
+ stackOut.push(stackIn.pop());
170
169
}
171
170
}
172
171
}
@@ -302,8 +301,8 @@ func (this *MyQueue) Empty() bool {
302
301
* Initialize your data structure here.
303
302
*/
304
303
var MyQueue = function () {
305
- this .stack1 = [];
306
- this .stack2 = [];
304
+ this .stackIn = [];
305
+ this .stackOut = [];
307
306
};
308
307
309
308
/**
@@ -312,22 +311,22 @@ var MyQueue = function() {
312
311
* @return {void}
313
312
*/
314
313
MyQueue .prototype .push = function (x ) {
315
- this .stack1 .push (x);
314
+ this .stackIn .push (x);
316
315
};
317
316
318
317
/**
319
318
* Removes the element from in front of queue and returns that element.
320
319
* @return {number}
321
320
*/
322
321
MyQueue .prototype .pop = function () {
323
- const size = this .stack2 .length ;
322
+ const size = this .stackOut .length ;
324
323
if (size) {
325
- return this .stack2 .pop ();
324
+ return this .stackOut .pop ();
326
325
}
327
- while (this .stack1 .length ) {
328
- this .stack2 .push (this .stack1 .pop ());
326
+ while (this .stackIn .length ) {
327
+ this .stackOut .push (this .stackIn .pop ());
329
328
}
330
- return this .stack2 .pop ();
329
+ return this .stackOut .pop ();
331
330
};
332
331
333
332
/**
@@ -336,7 +335,7 @@ MyQueue.prototype.pop = function() {
336
335
*/
337
336
MyQueue .prototype .peek = function () {
338
337
const x = this .pop ();
339
- this .stack2 .push (x);
338
+ this .stackOut .push (x);
340
339
return x;
341
340
};
342
341
@@ -345,7 +344,7 @@ MyQueue.prototype.peek = function() {
345
344
* @return {boolean}
346
345
*/
347
346
MyQueue .prototype .empty = function () {
348
- return ! this .stack1 .length && ! this .stack2 .length
347
+ return ! this .stackIn .length && ! this .stackOut .length
349
348
};
350
349
```
351
350
@@ -419,9 +418,9 @@ void myQueuePush(MyQueue* obj, int x) {
419
418
}
420
419
421
420
/*
422
- 1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stack2 [ stackTop2++] = stack1 [ --stackTop1] )
421
+ 1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stackOut [ stackTop2++] = stackIn [ --stackTop1] )
423
422
2.将栈顶元素保存
424
- 3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stack1 [ stackTop1++] = stack2 [ --stackTop2] )
423
+ 3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stackIn [ stackTop1++] = stackOut [ --stackTop2] )
425
424
* /
426
425
int myQueuePop(MyQueue* obj) {
427
426
//优化:复制栈顶指针,减少对内存的访问次数
0 commit comments