-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm622.py
52 lines (42 loc) · 1.28 KB
/
m622.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class MyCircularQueue:
def __init__(self, k: int):
self.vals = [0] * k
self.size = 0
self.pop = 0
self.add = 0
self.k = k
def enQueue(self, value: int) -> bool:
if self.size >= self.k :
return False
if self.size == 0 :
self.pop = 0
self.add = 0
self.vals[0] = value
else :
self.add = (self.add + 1) % self.k
self.vals[self.add] = value
self.size += 1
return True
def deQueue(self) -> bool:
if self.isEmpty() :
return False
if self.pop != self.add :
self.pop = (self.pop + 1) % self.k
self.size -= 1
return True
def Front(self) -> int:
return self.vals[self.pop] if self.size > 0 else -1
def Rear(self) -> int:
return self.vals[self.add] if self.size > 0 else -1
def isEmpty(self) -> bool:
return self.size == 0
def isFull(self) -> bool:
return self.size == self.k
# Your MyCircularQueue object will be instantiated and called as such:
# obj = MyCircularQueue(k)
# param_1 = obj.enQueue(value)
# param_2 = obj.deQueue()
# param_3 = obj.Front()
# param_4 = obj.Rear()
# param_5 = obj.isEmpty()
# param_6 = obj.isFull()