From e495a0fccef650dda96f4fec9b47c84f3ad7ed4e Mon Sep 17 00:00:00 2001 From: joeCarf <52153761+joeCarf@users.noreply.github.com> Date: Fri, 17 Dec 2021 15:03:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9232=E9=A2=98=E4=B8=AD?= =?UTF-8?q?Java=E5=86=99=E6=B3=95=E5=8F=8A=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 命名更易理解,写法更加简洁 --- problems/0232.用栈实现队列.md | 55 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index ec273651..7e0ba631 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -130,43 +130,42 @@ Java: ```java class MyQueue { - Stack stack1; - Stack stack2; + Stack stackIn; + Stack stackOut; /** Initialize your data structure here. */ public MyQueue() { - stack1 = new Stack<>(); // 负责进栈 - stack2 = new Stack<>(); // 负责出栈 + stackIn = new Stack<>(); // 负责进栈 + stackOut = new Stack<>(); // 负责出栈 } /** Push element x to the back of queue. */ public void push(int x) { - stack1.push(x); + stackIn.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { - dumpStack1(); - return stack2.pop(); + dumpstackIn(); + return stackOut.pop(); } /** Get the front element. */ public int peek() { - dumpStack1(); - return stack2.peek(); + dumpstackIn(); + return stackOut.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { - return stack1.isEmpty() && stack2.isEmpty(); + return stackIn.isEmpty() && stackOut.isEmpty(); } - // 如果stack2为空,那么将stack1中的元素全部放到stack2中 - private void dumpStack1(){ - if (stack2.isEmpty()){ - while (!stack1.isEmpty()){ - stack2.push(stack1.pop()); - } + // 如果stackOut为空,那么将stackIn中的元素全部放到stackOut中 + private void dumpstackIn(){ + if (!stackOut.isEmpty()) return; + while (!stackIn.isEmpty()){ + stackOut.push(stackIn.pop()); } } } @@ -302,8 +301,8 @@ func (this *MyQueue) Empty() bool { * Initialize your data structure here. */ var MyQueue = function() { - this.stack1 = []; - this.stack2 = []; + this.stackIn = []; + this.stackOut = []; }; /** @@ -312,7 +311,7 @@ var MyQueue = function() { * @return {void} */ MyQueue.prototype.push = function(x) { - this.stack1.push(x); + this.stackIn.push(x); }; /** @@ -320,14 +319,14 @@ MyQueue.prototype.push = function(x) { * @return {number} */ MyQueue.prototype.pop = function() { - const size = this.stack2.length; + const size = this.stackOut.length; if(size) { - return this.stack2.pop(); + return this.stackOut.pop(); } - while(this.stack1.length) { - this.stack2.push(this.stack1.pop()); + while(this.stackIn.length) { + this.stackOut.push(this.stackIn.pop()); } - return this.stack2.pop(); + return this.stackOut.pop(); }; /** @@ -336,7 +335,7 @@ MyQueue.prototype.pop = function() { */ MyQueue.prototype.peek = function() { const x = this.pop(); - this.stack2.push(x); + this.stackOut.push(x); return x; }; @@ -345,7 +344,7 @@ MyQueue.prototype.peek = function() { * @return {boolean} */ MyQueue.prototype.empty = function() { - return !this.stack1.length && !this.stack2.length + return !this.stackIn.length && !this.stackOut.length }; ``` @@ -419,9 +418,9 @@ void myQueuePush(MyQueue* obj, int x) { } /* -1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stack2[stackTop2++] = stack1[--stackTop1]) +1.若输出栈为空且当第一个栈中有元素(stackInTop>0时),将第一个栈中元素复制到第二个栈中(stackOut[stackTop2++] = stackIn[--stackTop1]) 2.将栈顶元素保存 -3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stack1[stackTop1++] = stack2[--stackTop2]) +3.当stackTop2>0时,将第二个栈中元素复制到第一个栈中(stackIn[stackTop1++] = stackOut[--stackTop2]) */ int myQueuePop(MyQueue* obj) { //优化:复制栈顶指针,减少对内存的访问次数