队头插入:每次直接压入左边的栈即可
队尾删除:若右栈不为空,则直接弹出一个值即可;若右栈为空,则将左栈先全部入右栈,再弹出一个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class CQueue:
def __init__(self): self.left_stack = list() self.right_stack = list()
def appendTail(self, value: int) -> None: self.left_stack.append(value)
def deleteHead(self) -> int: if not self.right_stack and not self.left_stack: return -1 if not self.right_stack: while self.left_stack: self.right_stack.append(self.left_stack.pop()) return self.right_stack.pop() else: return self.right_stack.pop()
|