class MyCircularDeque:
def __init__(self, k: int):
self.k, self.q = k, collections.deque()
def insertFront(self, value: int) -> bool:
return len(self.q) < self.k and (self.q.appendleft(value) or True)
def insertLast(self, value: int) -> bool:
return len(self.q) < self.k and (self.q.append(value) or True)
def deleteFront(self) -> bool:
return self.q and (self.q.popleft() or True)
def deleteLast(self) -> bool:
return self.q and (self.q.pop() or True)
def getFront(self) -> int:
return self.q[0] if self.q else -1
def getRear(self) -> int:
return self.q[-1] if self.q else -1
def isEmpty(self) -> bool:
return not len(self.q)
def isFull(self) -> bool:
return len(self.q) == self.k
# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()
中规中矩的写法,没什么亮点,只能勉强通过优化python3中函数的使用来减小时间