From 067c25f616e8bbc8e0e122a9c2aca50b208ece9a Mon Sep 17 00:00:00 2001 From: FlySmile <870467813@qq.com> Date: Sat, 26 Nov 2022 19:57:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit about go版本 --- problems/0232.用栈实现队列.md | 65 +++++++++++++---------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 5858f9e0..1f7d236d 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -231,56 +231,51 @@ class MyQueue: Go: ```Go type MyQueue struct { - stack []int - back []int + stackIn []int //输入栈 + stackOut []int //输出栈 } -/** Initialize your data structure here. */ func Constructor() MyQueue { - return MyQueue{ - stack: make([]int, 0), - back: make([]int, 0), - } + return MyQueue{ + stackIn: make([]int, 0), + stackOut: make([]int, 0), + } } -/** Push element x to the back of queue. */ +// 往输入栈做push func (this *MyQueue) Push(x int) { - for len(this.back) != 0 { - val := this.back[len(this.back)-1] - this.back = this.back[:len(this.back)-1] - this.stack = append(this.stack, val) - } - this.stack = append(this.stack, x) + this.stackIn = append(this.stackIn, x) } -/** Removes the element from in front of queue and returns that element. */ +// 在输出栈做pop,pop时如果输出栈数据为空,需要将输入栈全部数据导入,如果非空,则可直接使用 func (this *MyQueue) Pop() int { - for len(this.stack) != 0 { - val := this.stack[len(this.stack)-1] - this.stack = this.stack[:len(this.stack)-1] - this.back = append(this.back, val) - } - if len(this.back) == 0 { - return 0 - } - val := this.back[len(this.back)-1] - this.back = this.back[:len(this.back)-1] - return val + inLen, outLen := len(this.stackIn), len(this.stackOut) + if outLen == 0 { + if inLen == 0 { + return -1 + } + for i := inLen - 1; i >= 0; i-- { + this.stackOut = append(this.stackOut, this.stackIn[i]) + } + this.stackIn = []int{} //导出后清空 + outLen = len(this.stackOut) //更新长度值 + } + val := this.stackOut[outLen-1] + this.stackOut = this.stackOut[:outLen-1] + return val } -/** Get the front element. */ func (this *MyQueue) Peek() int { - val := this.Pop() - if val == 0 { - return 0 - } - this.back = append(this.back, val) - return val + val := this.Pop() + if val == -1 { + return -1 + } + this.stackOut = append(this.stackOut, val) + return val } -/** Returns whether the queue is empty. */ func (this *MyQueue) Empty() bool { - return len(this.stack) == 0 && len(this.back) == 0 + return len(this.stackIn) == 0 && len(this.stackOut) == 0 } ``` From b486f8dc20899486331385df08d9e9e0db41d906 Mon Sep 17 00:00:00 2001 From: FlySmile <870467813@qq.com> Date: Sat, 26 Nov 2022 20:01:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200232.=E7=94=A8=E6=A0=88=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=98=9F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit about go版本 --- problems/0232.用栈实现队列.md | 56 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 1f7d236d..d2211d11 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -231,51 +231,51 @@ class MyQueue: Go: ```Go type MyQueue struct { - stackIn []int //输入栈 - stackOut []int //输出栈 + stackIn []int //输入栈 + stackOut []int //输出栈 } func Constructor() MyQueue { - return MyQueue{ - stackIn: make([]int, 0), - stackOut: make([]int, 0), - } + return MyQueue{ + stackIn: make([]int, 0), + stackOut: make([]int, 0), + } } // 往输入栈做push func (this *MyQueue) Push(x int) { - this.stackIn = append(this.stackIn, x) + this.stackIn = append(this.stackIn, x) } // 在输出栈做pop,pop时如果输出栈数据为空,需要将输入栈全部数据导入,如果非空,则可直接使用 func (this *MyQueue) Pop() int { - inLen, outLen := len(this.stackIn), len(this.stackOut) - if outLen == 0 { - if inLen == 0 { - return -1 - } - for i := inLen - 1; i >= 0; i-- { - this.stackOut = append(this.stackOut, this.stackIn[i]) - } - this.stackIn = []int{} //导出后清空 - outLen = len(this.stackOut) //更新长度值 - } - val := this.stackOut[outLen-1] - this.stackOut = this.stackOut[:outLen-1] - return val + inLen, outLen := len(this.stackIn), len(this.stackOut) + if outLen == 0 { + if inLen == 0 { + return -1 + } + for i := inLen - 1; i >= 0; i-- { + this.stackOut = append(this.stackOut, this.stackIn[i]) + } + this.stackIn = []int{} //导出后清空 + outLen = len(this.stackOut) //更新长度值 + } + val := this.stackOut[outLen-1] + this.stackOut = this.stackOut[:outLen-1] + return val } func (this *MyQueue) Peek() int { - val := this.Pop() - if val == -1 { - return -1 - } - this.stackOut = append(this.stackOut, val) - return val + val := this.Pop() + if val == -1 { + return -1 + } + this.stackOut = append(this.stackOut, val) + return val } func (this *MyQueue) Empty() bool { - return len(this.stackIn) == 0 && len(this.stackOut) == 0 + return len(this.stackIn) == 0 && len(this.stackOut) == 0 } ```