From fa00cd53b597d9ce837319f1967d0ae6478c430e Mon Sep 17 00:00:00 2001 From: lingyin Date: Wed, 6 Apr 2022 10:28:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96Peek=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E5=A4=8D=E7=94=A8Pop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0232.用栈实现队列.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index b00ffd80..1a56d9f3 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -275,15 +275,11 @@ func (this *MyQueue) Pop() int { /** Get the front element. */ func (this *MyQueue) Peek() 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 { + val := this.Pop() + if val == 0 { return 0 } - val := this.back[len(this.back)-1] + this.back = append(this.back, val) return val }