1 class MyQueue: 2 3 def __init__(self): 4 """ 5 Initialize your data structure here. 6 """ 7 self.mainstack = [] 8 self.helpstack = [] 9 10 def push(self, x: int) -> None: 11 """ 12 Push element x to the back of queue. 13 """ 14 self.mainstack += self.helpstack 15 self.mainstack.append(x) 16 self.helpstack = [] 17 18 def pop(self) -> int: 19 """ 20 Removes the element from in front of queue and returns that element. 21 """ 22 front = self.mainstack[0] 23 self.mainstack = self.mainstack[1:] 24 return front 25 26 def peek(self) -> int: 27 """ 28 Get the front element. 29 """ 30 return self.mainstack[0] 31 32 def empty(self) -> bool: 33 """ 34 Returns whether the queue is empty. 35 """ 36 return len(self.mainstack) < 1 37 38 39 # Your MyQueue object will be instantiated and called as such: 40 # obj = MyQueue() 41 # obj.push(x) 42 # param_2 = obj.pop() 43 # param_3 = obj.peek() 44 # param_4 = obj.empty()
原文:https://www.cnblogs.com/lj95/p/14393435.html