mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
更正 0232.用栈实现队列.md python代码
添加限制条件,仅在栈不为空的情况下才允许后续pop()和peek()的操作
This commit is contained in:
@ -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:
|
||||
|
Reference in New Issue
Block a user