-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.py
More file actions
186 lines (156 loc) · 5.75 KB
/
deque.py
File metadata and controls
186 lines (156 loc) · 5.75 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'''
两种设置head和tail的思路
第一种:
head 和 tail 都指向当前对应的头和尾。问题是无法通过 head 和 tail 的位置来计算队列的长度。
length = (tail - head + 1) % size
1.
head tail
+----+----+----+----+----+----+----+----+----+----+----+
| | | | 1 | 2 | 3 | 4 | 5 | | | |
+----+----+----+----+----+----+----+----+----+----+----+
length = (7 - 3 + 1) % 11 = 5
2.
tail head
+----+----+----+----+----+----+----+----+----+----+----+
| 5 | 6 | 7 | 8 | | | | 1 | 2 | 3 | 4 |
+----+----+----+----+----+----+----+----+----+----+----+
length = (3 - 7 + 1) % 11 = 8
3.
tail head
+----+----+----+----+----+----+----+----+----+----+----+
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 1 | 2 | 3 | 4 |
+----+----+----+----+----+----+----+----+----+----+----+
length = (6 - 7 + 1) % 11 = 0
4.
tail head
+----+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+----+
length = (6 - 7 + 1) % 11 = 0
上面 1 和 2 两种情况是通过公式计算出的长度是正确的。3 和 4两种情况则是混淆不清的,当队列长度等于0和等于总长度size时,公式计算结果都是0。
一种解决办法是使用一个变量保存队列的长度。
第二种:
head 指向当前头部的前一个位置(即下次往头部插入元素的位置),tail 指向当前尾部的后一个位置(即下次往尾部插入元素的位置)。另外,队列的长度初始化为比原来长度大 1。
这种方法解决了第一种方法的问题。
length = (tail - head - 1) % size
1.
head tail
+----+----+----+----+----+----+----+----+----+----+----+----+
| | | | 1 | 2 | 3 | 4 | 5 | | | | | (size = 11 + 1 = 12)
+----+----+----+----+----+----+----+----+----+----+----+----+
length = (8 - 2 - 1) % 12 = 5
2.
tail head
+----+----+----+----+----+----+----+----+----+----+----+----+
| 5 | 6 | 7 | 8 | | | | | 1 | 2 | 3 | 4 |
+----+----+----+----+----+----+----+----+----+----+----+----+
length = (4 - 7 - 1) % 12 = 8
3.
tail(head)
+----+----+----+----+----+----+----+----+----+----+----+----+
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 1 | 2 | 3 | 4 |
+----+----+----+----+----+----+----+----+----+----+----+----+
length = (7 - 7 - 1) % 12 = 11
4.
head tail
+----+----+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+----+----+
length = (7 - 6 - 1) % 12 = 0
'''
class MyCircularDeque:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the deque to be k.
"""
self.size = k
self.length = 0
self.queue = [None] * self.size
self.head = 0
self.tail = 0
def _reset(self):
self.head = 0
self.tail = 0
def insertFront(self, value: int) -> bool:
"""
Adds an item at the front of Deque. Return true if the operation is successful.
"""
if self.isFull():
return False
if self.isEmpty():
self.head = 1
self.tail = 0
self.head = (self.head - 1) % self.size
self.queue[self.head] = value
self.length += 1
return True
def insertLast(self, value: int) -> bool:
"""
Adds an item at the rear of Deque. Return true if the operation is successful.
"""
if self.isFull():
return False
if self.isEmpty():
self.head = 0
self.tail = -1
self.tail = (self.tail + 1) % self.size
self.queue[self.tail] = value
self.length += 1
return True
def deleteFront(self) -> bool:
"""
Deletes an item from the front of Deque. Return true if the operation is successful.
"""
if self.isEmpty():
return False
val = self.queue[self.head]
self.head = (self.head + 1) % self.size
self.length -= 1
return True
def deleteLast(self) -> bool:
"""
Deletes an item from the rear of Deque. Return true if the operation is successful.
"""
if self.isEmpty():
return False
val = self.queue[self.tail]
self.tail = (self.tail - 1) % self.size
self.length -= 1
return True
def getFront(self) -> int:
"""
Get the front item from the deque.
"""
if self.isEmpty():
return -1
return self.queue[self.head]
def getRear(self) -> int:
"""
Get the last item from the deque.
"""
if self.isEmpty():
return -1
return self.queue[self.tail]
def isEmpty(self) -> bool:
"""
Checks whether the circular deque is empty or not.
"""
return self.length == 0
def isFull(self) -> bool:
"""
Checks whether the circular deque is full or not.
"""
return self.length == self.size
# Your MyCircularDeque object will be instantiated and called as such:
if __name__ == '__main__':
obj = MyCircularDeque(10)
param_1 = obj.insertFront(1)
param_2 = obj.insertLast(2)
param_2 = obj.insertLast(3)
param_2 = obj.insertLast(4)
param_3 = obj.deleteFront()
param_4 = obj.deleteLast()
param_5 = obj.getFront()
param_6 = obj.getRear()
param_7 = obj.isEmpty()
param_8 = obj.isFull()