Skip to content

Commit a53d69a

Browse files
authored
Create design-circular-deque.py
1 parent 5735304 commit a53d69a

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Python/design-circular-deque.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Time: O(1)
2+
# Space: O(k)
3+
4+
# Design your implementation of the circular double-ended queue (deque).
5+
# Your implementation should support following operations:
6+
#
7+
# MyCircularDeque(k): Constructor, set the size of the deque to be k.
8+
# insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
9+
# insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
10+
# deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
11+
# deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
12+
# getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
13+
# getRear(): Gets the last item from Deque. If the deque is empty, return -1.
14+
# isEmpty(): Checks whether Deque is empty or not.
15+
# isFull(): Checks whether Deque is full or not.
16+
# Example:
17+
#
18+
# MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
19+
# circularDeque.insertLast(1); // return true
20+
# circularDeque.insertLast(2); // return true
21+
# circularDeque.insertFront(3); // return true
22+
# circularDeque.insertFront(4); // return false, the queue is full
23+
# circularDeque.getRear(); // return 32
24+
# circularDeque.isFull(); // return true
25+
# circularDeque.deleteLast(); // return true
26+
# circularDeque.insertFront(4); // return true
27+
# circularDeque.getFront(); // return 4
28+
#
29+
# Note:
30+
# - All values will be in the range of [1, 1000].
31+
# - The number of operations will be in the range of [1, 1000].
32+
# - Please do not use the built-in Deque library.
33+
34+
class MyCircularDeque(object):
35+
36+
def __init__(self, k):
37+
"""
38+
Initialize your data structure here. Set the size of the deque to be k.
39+
:type k: int
40+
"""
41+
self.__start = 0
42+
self.__size = 0
43+
self.__buffer = [0] * k
44+
45+
def insertFront(self, value):
46+
"""
47+
Adds an item at the front of Deque. Return true if the operation is successful.
48+
:type value: int
49+
:rtype: bool
50+
"""
51+
if self.isFull():
52+
return False
53+
self.__start = (self.__start-1) % len(self.__buffer)
54+
self.__buffer[self.__start] = value
55+
self.__size += 1
56+
return True
57+
58+
def insertLast(self, value):
59+
"""
60+
Adds an item at the rear of Deque. Return true if the operation is successful.
61+
:type value: int
62+
:rtype: bool
63+
"""
64+
if self.isFull():
65+
return False
66+
self.__buffer[(self.__start+self.__size) % len(self.__buffer)] = value
67+
self.__size += 1
68+
return True
69+
70+
def deleteFront(self):
71+
"""
72+
Deletes an item from the front of Deque. Return true if the operation is successful.
73+
:rtype: bool
74+
"""
75+
if self.isEmpty():
76+
return False
77+
self.__start = (self.__start+1) % len(self.__buffer)
78+
self.__size -= 1
79+
return True
80+
81+
def deleteLast(self):
82+
"""
83+
Deletes an item from the rear of Deque. Return true if the operation is successful.
84+
:rtype: bool
85+
"""
86+
if self.isEmpty():
87+
return False
88+
self.__size -= 1
89+
return True
90+
91+
def getFront(self):
92+
"""
93+
Get the front item from the deque.
94+
:rtype: int
95+
"""
96+
return -1 if self.isEmpty() else self.__buffer[self.__start]
97+
98+
def getRear(self):
99+
"""
100+
Get the last item from the deque.
101+
:rtype: int
102+
"""
103+
return -1 if self.isEmpty() else self.__buffer[(self.__start+self.__size-1) % len(self.__buffer)]
104+
105+
def isEmpty(self):
106+
"""
107+
Checks whether the circular deque is empty or not.
108+
:rtype: bool
109+
"""
110+
return self.__size == 0
111+
112+
def isFull(self):
113+
"""
114+
Checks whether the circular deque is full or not.
115+
:rtype: bool
116+
"""
117+
return self.__size == len(self.__buffer)
118+
119+
120+
# Your MyCircularDeque object will be instantiated and called as such:
121+
# obj = MyCircularDeque(k)
122+
# param_1 = obj.insertFront(value)
123+
# param_2 = obj.insertLast(value)
124+
# param_3 = obj.deleteFront()
125+
# param_4 = obj.deleteLast()
126+
# param_5 = obj.getFront()
127+
# param_6 = obj.getRear()
128+
# param_7 = obj.isEmpty()
129+
# param_8 = obj.isFull()

0 commit comments

Comments
 (0)