Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e495a0f

Browse files
committed
feat: 修改232题中Java写法及命名
命名更易理解,写法更加简洁
1 parent b137d79 commit e495a0f

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

problems/0232.用栈实现队列.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -130,43 +130,42 @@ Java:
130130
```java
131131
class MyQueue {
132132
133-
Stack<Integer> stack1;
134-
Stack<Integer> stack2;
133+
Stack<Integer> stackIn;
134+
Stack<Integer> stackOut;
135135
136136
/** Initialize your data structure here. */
137137
public MyQueue() {
138-
stack1 = new Stack<>(); // 负责进栈
139-
stack2 = new Stack<>(); // 负责出栈
138+
stackIn = new Stack<>(); // 负责进栈
139+
stackOut = new Stack<>(); // 负责出栈
140140
}
141141
142142
/** Push element x to the back of queue. */
143143
public void push(int x) {
144-
stack1.push(x);
144+
stackIn.push(x);
145145
}
146146
147147
/** Removes the element from in front of queue and returns that element. */
148148
public int pop() {
149-
dumpStack1();
150-
return stack2.pop();
149+
dumpstackIn();
150+
return stackOut.pop();
151151
}
152152
153153
/** Get the front element. */
154154
public int peek() {
155-
dumpStack1();
156-
return stack2.peek();
155+
dumpstackIn();
156+
return stackOut.peek();
157157
}
158158
159159
/** Returns whether the queue is empty. */
160160
public boolean empty() {
161-
return stack1.isEmpty() && stack2.isEmpty();
161+
return stackIn.isEmpty() && stackOut.isEmpty();
162162
}
163163
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());
170169
}
171170
}
172171
}
@@ -302,8 +301,8 @@ func (this *MyQueue) Empty() bool {
302301
* Initialize your data structure here.
303302
*/
304303
var MyQueue = function() {
305-
this.stack1 = [];
306-
this.stack2 = [];
304+
this.stackIn = [];
305+
this.stackOut = [];
307306
};
308307

309308
/**
@@ -312,22 +311,22 @@ var MyQueue = function() {
312311
* @return {void}
313312
*/
314313
MyQueue.prototype.push = function(x) {
315-
this.stack1.push(x);
314+
this.stackIn.push(x);
316315
};
317316

318317
/**
319318
* Removes the element from in front of queue and returns that element.
320319
* @return {number}
321320
*/
322321
MyQueue.prototype.pop = function() {
323-
const size = this.stack2.length;
322+
const size = this.stackOut.length;
324323
if(size) {
325-
return this.stack2.pop();
324+
return this.stackOut.pop();
326325
}
327-
while(this.stack1.length) {
328-
this.stack2.push(this.stack1.pop());
326+
while(this.stackIn.length) {
327+
this.stackOut.push(this.stackIn.pop());
329328
}
330-
return this.stack2.pop();
329+
return this.stackOut.pop();
331330
};
332331

333332
/**
@@ -336,7 +335,7 @@ MyQueue.prototype.pop = function() {
336335
*/
337336
MyQueue.prototype.peek = function() {
338337
const x = this.pop();
339-
this.stack2.push(x);
338+
this.stackOut.push(x);
340339
return x;
341340
};
342341

@@ -345,7 +344,7 @@ MyQueue.prototype.peek = function() {
345344
* @return {boolean}
346345
*/
347346
MyQueue.prototype.empty = function() {
348-
return !this.stack1.length && !this.stack2.length
347+
return !this.stackIn.length && !this.stackOut.length
349348
};
350349
```
351350

@@ -419,9 +418,9 @@ void myQueuePush(MyQueue* obj, int x) {
419418
}
420419

421420
/*
422-
1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stack2[stackTop2++] = stack1[--stackTop1])
421+
1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stackOut[stackTop2++] = stackIn[--stackTop1])
423422
2.将栈顶元素保存
424-
3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stack1[stackTop1++] = stack2[--stackTop2])
423+
3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stackIn[stackTop1++] = stackOut[--stackTop2])
425424
*/
426425
int myQueuePop(MyQueue* obj) {
427426
//优化:复制栈顶指针,减少对内存的访问次数

0 commit comments

Comments
 (0)