-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdesign-circular-deque.py
More file actions
91 lines (72 loc) · 2.08 KB
/
Copy pathdesign-circular-deque.py
File metadata and controls
91 lines (72 loc) · 2.08 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class MyCircularDeque:
_data: list[int]
_head: int
_tail: int
_count: int
def __init__(self, k: int):
self._data = [0] * k
self._head = 0
self._tail = 0
self._count = 0
def insertFront(self, value: int) -> bool:
# check is not full
if self.isFull():
return False
# move the head pointer
self._head -= 1
self._head %= len(self._data)
# set the value
self._data[self._head] = value
self._count += 1
return True
def insertLast(self, value: int) -> bool:
# check is not full
if self.isFull():
return False
# set the value
self._data[self._tail] = value
# move the tail pointer
self._tail += 1
self._tail %= len(self._data)
self._count += 1
return True
def deleteFront(self) -> bool:
# check is not empty
if self.isEmpty():
return False
# move the head pointer
self._head += 1
self._head %= len(self._data)
self._count -= 1
return True
def deleteLast(self) -> bool:
# check is not empty
if self.isEmpty():
return False
# move the tail pointer
self._tail -= 1
self._tail %= len(self._data)
self._count -= 1
return True
def getFront(self) -> int:
if self.isEmpty():
return -1
return self._data[self._head]
def getRear(self) -> int:
if self.isEmpty():
return -1
return self._data[(self._tail - 1) % len(self._data)]
def isEmpty(self) -> bool:
return self._count == 0
def isFull(self) -> bool:
return self._count == len(self._data)
# 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()