-
Notifications
You must be signed in to change notification settings - Fork 0
/
design_circular_deque.py
62 lines (52 loc) · 1.7 KB
/
design_circular_deque.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
53
54
55
56
57
58
59
60
61
62
class MyCircularDeque:
def __init__(self, k: int):
# front means left and rear mean right
self.deque = [0] * k
self.front = 0
self.rear = 0
self.size = k
self.curr_size = 0
def insertFront(self, value: int) -> bool:
if self.isFull():
return False
if not self.isEmpty():
self.front = ((self.front - 1) % self.size)
self.deque[self.front] = value
self.curr_size += 1
return True
def insertLast(self, value: int) -> bool:
if self.isFull():
return False
if not self.isEmpty():
self.rear = ((self.rear + 1) % self.size)
self.deque[self.rear] = value
self.curr_size += 1
return True
def deleteFront(self) -> bool:
if self.isEmpty():
return False
self.front = ((self.front + 1) % self.size)
self.curr_size -=1
if self.isEmpty():
self.front = self.rear
return True
def deleteLast(self) -> bool:
if self.isEmpty():
return False
self.rear = ((self.rear - 1) % self.size)
self.curr_size -=1
if self.isEmpty():
self.front = self.rear
return True
def getFront(self) -> int:
if self.isEmpty():
return -1
return self.deque[self.front]
def getRear(self) -> int:
if self.isEmpty():
return -1
return self.deque[self.rear]
def isEmpty(self) -> bool:
return self.curr_size == 0
def isFull(self) -> bool:
return self.curr_size == self.size