@@ -45,27 +45,214 @@ circularDeque.getFront(); // 返回 4
45
45
<li>请不要使用内置的双端队列库。</li>
46
46
</ul >
47
47
48
-
49
48
## 解法
50
49
51
50
<!-- 这里可写通用的实现逻辑 -->
52
51
52
+ “循环数组”实现。
53
+
53
54
<!-- tabs:start -->
54
55
55
56
### ** Python3**
56
57
57
58
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
59
59
60
``` python
61
+ class MyCircularDeque :
62
+
63
+ def __init__ (self , k : int ):
64
+ """
65
+ Initialize your data structure here. Set the size of the deque to be k.
66
+ """
67
+ self .q = [0 ] * k
68
+ self .front = 0
69
+ self .size = 0
70
+ self .capacity = k
71
+
72
+ def insertFront (self , value : int ) -> bool :
73
+ """
74
+ Adds an item at the front of Deque. Return true if the operation is successful.
75
+ """
76
+ if self .isFull():
77
+ return False
78
+ if not self .isEmpty():
79
+ self .front = (self .front - 1 + self .capacity) % self .capacity
80
+ self .q[self .front] = value
81
+ self .size += 1
82
+ return True
83
+
84
+ def insertLast (self , value : int ) -> bool :
85
+ """
86
+ Adds an item at the rear of Deque. Return true if the operation is successful.
87
+ """
88
+ if self .isFull():
89
+ return False
90
+ idx = (self .front + self .size) % self .capacity
91
+ self .q[idx] = value
92
+ self .size += 1
93
+ return True
94
+
95
+ def deleteFront (self ) -> bool :
96
+ """
97
+ Deletes an item from the front of Deque. Return true if the operation is successful.
98
+ """
99
+ if self .isEmpty():
100
+ return False
101
+ self .front = (self .front + 1 ) % self .capacity
102
+ self .size -= 1
103
+ return True
104
+
105
+ def deleteLast (self ) -> bool :
106
+ """
107
+ Deletes an item from the rear of Deque. Return true if the operation is successful.
108
+ """
109
+ if self .isEmpty():
110
+ return False
111
+ self .size -= 1
112
+ return True
113
+
114
+ def getFront (self ) -> int :
115
+ """
116
+ Get the front item from the deque.
117
+ """
118
+ if self .isEmpty():
119
+ return - 1
120
+ return self .q[self .front]
121
+
122
+ def getRear (self ) -> int :
123
+ """
124
+ Get the last item from the deque.
125
+ """
126
+ if self .isEmpty():
127
+ return - 1
128
+ idx = (self .front + self .size - 1 ) % self .capacity
129
+ return self .q[idx]
130
+
131
+ def isEmpty (self ) -> bool :
132
+ """
133
+ Checks whether the circular deque is empty or not.
134
+ """
135
+ return self .size == 0
136
+
137
+ def isFull (self ) -> bool :
138
+ """
139
+ Checks whether the circular deque is full or not.
140
+ """
141
+ return self .size == self .capacity
60
142
143
+
144
+ # Your MyCircularDeque object will be instantiated and called as such:
145
+ # obj = MyCircularDeque(k)
146
+ # param_1 = obj.insertFront(value)
147
+ # param_2 = obj.insertLast(value)
148
+ # param_3 = obj.deleteFront()
149
+ # param_4 = obj.deleteLast()
150
+ # param_5 = obj.getFront()
151
+ # param_6 = obj.getRear()
152
+ # param_7 = obj.isEmpty()
153
+ # param_8 = obj.isFull()
61
154
```
62
155
63
156
### ** Java**
64
157
65
158
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
159
67
160
``` java
161
+ class MyCircularDeque {
162
+ private int [] q;
163
+ private int front;
164
+ private int size;
165
+ private int capacity;
166
+
167
+ /* * Initialize your data structure here. Set the size of the deque to be k. */
168
+ public MyCircularDeque (int k ) {
169
+ q = new int [k];
170
+ capacity = k;
171
+ }
172
+
173
+ /* * Adds an item at the front of Deque. Return true if the operation is successful. */
174
+ public boolean insertFront (int value ) {
175
+ if (isFull()) {
176
+ return false ;
177
+ }
178
+ if (! isEmpty()) {
179
+ front = (front - 1 + capacity) % capacity;
180
+ }
181
+ q[front] = value;
182
+ ++ size;
183
+ return true ;
184
+ }
185
+
186
+ /* * Adds an item at the rear of Deque. Return true if the operation is successful. */
187
+ public boolean insertLast (int value ) {
188
+ if (isFull()) {
189
+ return false ;
190
+ }
191
+ int idx = (front + size) % capacity;
192
+ q[idx] = value;
193
+ ++ size;
194
+ return true ;
195
+ }
196
+
197
+ /* * Deletes an item from the front of Deque. Return true if the operation is successful. */
198
+ public boolean deleteFront () {
199
+ if (isEmpty()) {
200
+ return false ;
201
+ }
202
+ front = (front + 1 ) % capacity;
203
+ -- size;
204
+ return true ;
205
+ }
206
+
207
+ /* * Deletes an item from the rear of Deque. Return true if the operation is successful. */
208
+ public boolean deleteLast () {
209
+ if (isEmpty()) {
210
+ return false ;
211
+ }
212
+ -- size;
213
+ return true ;
214
+ }
215
+
216
+ /* * Get the front item from the deque. */
217
+ public int getFront () {
218
+ if (isEmpty()) {
219
+ return - 1 ;
220
+ }
221
+ return q[front];
222
+ }
223
+
224
+ /* * Get the last item from the deque. */
225
+ public int getRear () {
226
+ if (isEmpty()) {
227
+ return - 1 ;
228
+ }
229
+ int idx = (front + size - 1 ) % capacity;
230
+ return q[idx];
231
+ }
232
+
233
+ /* * Checks whether the circular deque is empty or not. */
234
+ public boolean isEmpty () {
235
+ return size == 0 ;
236
+ }
237
+
238
+ /* * Checks whether the circular deque is full or not. */
239
+ public boolean isFull () {
240
+ return size == capacity;
241
+ }
242
+ }
68
243
244
+ /**
245
+ * Your MyCircularDeque object will be instantiated and called as such:
246
+ * MyCircularDeque obj = new MyCircularDeque(k);
247
+ * boolean param_1 = obj.insertFront(value);
248
+ * boolean param_2 = obj.insertLast(value);
249
+ * boolean param_3 = obj.deleteFront();
250
+ * boolean param_4 = obj.deleteLast();
251
+ * int param_5 = obj.getFront();
252
+ * int param_6 = obj.getRear();
253
+ * boolean param_7 = obj.isEmpty();
254
+ * boolean param_8 = obj.isFull();
255
+ */
69
256
```
70
257
71
258
### ** ...**
0 commit comments