-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0232 [E] Implement Queue using Stacks
Code with Senpai edited this page Jul 2, 2022
·
1 revision
class MyQueue:
def __init__(self):
self.stPush = []
self.stPop = []
def push(self, x: int) -> None:
self.stPush.append(x)
def _transferPushToPop(self):
if not len(self.stPop):
while self.stPush:
self.stPop.append(self.stPush.pop())
def pop(self) -> int:
self._transferPushToPop()
return self.stPop.pop()
def peek(self) -> int:
self._transferPushToPop()
return self.stPop[-1]
def empty(self) -> bool:
return not (len(self.stPop) + len(self.stPush))
footer