-
Notifications
You must be signed in to change notification settings - Fork 1
/
Design Circular Deque.py
91 lines (74 loc) · 1.62 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
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(object):
def __init__(self, k):
"""
:type k: int
"""
self.items = []
self.k = k
def insertFront(self, value):
"""
:type value: int
:rtype: bool
"""
if self.isFull():
return False
else:
self.items.insert(0,value)
return True
def insertLast(self, value):
"""
:type value: int
:rtype: bool
"""
if self.isFull():
return False
else:
self.items.append(value)
return True
def deleteFront(self):
"""
:rtype: bool
"""
if self.isEmpty():
return False
else:
self.items.pop(0)
return True
def deleteLast(self):
"""
:rtype: bool
"""
if self.isEmpty():
return False
else:
self.items.pop()
return True
def getFront(self):
"""
:rtype: int
"""
if not self.isEmpty():
return self.items[0]
else:
return -1
def getRear(self):
"""
:rtype: int
"""
if not self.isEmpty():
return self.items[-1]
else:
return -1
def isEmpty(self):
"""
:rtype: bool
"""
if not self.items:
return True
else:
return False
def isFull(self):
"""
:rtype: bool
"""
return len(self.items) == self.k