From ca576c5bd0a62769c4b080028e7092d7fb945890 Mon Sep 17 00:00:00 2001 From: Eyjan_Huang <81480748+Eyjan-Huang@users.noreply.github.com> Date: Fri, 20 Aug 2021 03:29:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=200232.=E7=94=A8=E6=A0=88?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=98=9F=E5=88=97.md=20python=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加限制条件,仅在栈不为空的情况下才允许后续pop()和peek()的操作 --- problems/0232.用栈实现队列.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md index 46d884d3..27a3de4e 100644 --- a/problems/0232.用栈实现队列.md +++ b/problems/0232.用栈实现队列.md @@ -304,6 +304,9 @@ class MyQueue: 2. 如果out没有元素,就把in里面的元素(除了第一个)依次pop后装进out里面 3. 直接把in剩下的元素pop出来,就是queue头部的 """ + if self.empty: + return None + if self.stack_out: return self.stack_out.pop() else: @@ -317,6 +320,9 @@ class MyQueue: 1. 查out有没有元素,有就把最上面的返回 2. 如果out没有元素,就把in最下面的返回 """ + if self.empty: + return None + if self.stack_out: return self.stack_out[-1] else: