Design Circular Queue LeetCode solution in Python

1
class MyCircularQueue:
2
def __init__(self, k: int):
3
self.data = [0] * k
4
self.maxSize = k
5
self.head = 0
6
self.tail = -1
7

8
def enQueue(self, val: int) -> bool:
9
if self.isFull():
10
return False
11
self.tail = (self.tail + 1) % self.maxSize
12
self.data[self.tail] = val
13
return True
14

15
def deQueue(self) -> bool:
16
if self.isEmpty():
17
return False
18
if self.head == self.tail:
19
self.head, self.tail = 0, -1
20
else:
21
self.head = (self.head + 1) % self.maxSize
22
return True
23

24
def Front(self) -> int:
25
return -1 if self.isEmpty() else self.data[self.head]
26

27
def Rear(self) -> int:
28
return -1 if self.isEmpty() else self.data[self.tail]
29

30
def isEmpty(self) -> bool:
31
return self.tail == -1
32

33
def isFull(self) -> bool:
34
return not self.isEmpty() and (self.tail + 1) % self.maxSize == self.head

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0