classMyQueue: def__init__(self): """ Initialize your data structure here. """ self.master = list() self.aux = list() defpush(self, x: int) -> None: """ Push element x to the back of queue. """ iflen(self.master) == 0: self.master.append(x) # 入栈 else: while self.master: temp = self.master.pop() self.aux.append(temp) self.master.append(x) while self.aux: temp = self.aux.pop() self.master.append(temp)
defpop(self) -> int: """ Removes the element from in front of queue and returns that element. """ return self.master.pop()
defpeek(self) -> int: """ Get the front element. """ return self.master[-1]
defempty(self) -> bool: """ Returns whether the queue is empty. """ returnTrueiflen(self.master) == 0elseFalse
classMyQueue: def__init__(self): """ Initialize your data structure here. """ self.master = list() self.aux = list()
defpush(self, x: int) -> None: """ Push element x to the back of queue. """ self.master.append(x)
defpop(self) -> int: """ Removes the element from in front of queue and returns that element. """ iflen(self.aux) != 0: return self.aux.pop() else: while self.master: temp = self.master.pop() self.aux.append(temp) return self.aux.pop()
defpeek(self) -> int: """ Get the front element. """ iflen(self.aux) != 0: return self.aux[-1] else: while self.master: temp = self.master.pop() self.aux.append(temp) return self.aux[-1]
defempty(self) -> bool: """ Returns whether the queue is empty. """ returnTrueiflen(self.master)+len(self.aux) == 0elseFalse