The title
Implement a queue with two stacks. Implement appendTail and deleteHead to insert an integer at the end of the queue and delete an integer at the head of the queue. (If there are no elements in the queue, the deleteHead operation returns -1)
Example 1: Enter: ["CQueue"."appendTail"."deleteHead"."deleteHead"] [[],[3],[] output: [NULL, NULL,3,-1] Example 2: Input: ["CQueue"."deleteHead"."appendTail"."appendTail"."deleteHead"."deleteHead"[[]], [], [5], [2], [], []] output: [null, 1, null, null, 5, 2]Copy the code
Tip:
1 <= values <= 10000 Calls to appendTail and deleteHead are performed at most 10000 times
Their thinking
class CQueue:
def __init__(self):
self.queueListOut = []
self.queueListTemp = []
def appendTail(self, value: int) -> None:
self.queueListTemp.append(value)
def deleteHead(self) -> int:
if len(self.queueListOut) <= 0 and len(self.queueListTemp)<= 0: return- 1iflen(self.queueListOut) ! = 0:return self.queueListOut.pop()
whilelen(self.queueListTemp) ! = 0: self.queueListOut.append(self.queueListTemp.pop())return self.queueListOut.pop()
Copy the code