Python | Leetcode Python题解之第232题用栈实现队列

avatar
作者
筋斗云
阅读量:4

题目:

题解:

class MyQueue:      def __init__(self):         self.A, self.B = [], []      def push(self, x: int) -> None:         self.A.append(x)      def pop(self) -> int:         peek = self.peek()         self.B.pop()         return peek      def peek(self) -> int:         if self.B: return self.B[-1]         if not self.A: return -1         # 将栈 A 的元素依次移动至栈 B         while self.A:             self.B.append(self.A.pop())         return self.B[-1]      def empty(self) -> bool:         return not self.A and not self.B

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!