Skip to content

Commit 4c1f48e

Browse files
committed
feat: add solutions to lc problem: No.0641
No.0641.Design Circular Deque
1 parent 99127d0 commit 4c1f48e

File tree

4 files changed

+527
-1
lines changed

4 files changed

+527
-1
lines changed

solution/0600-0699/0641.Design Circular Deque/README.md

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ circularDeque.getFront(); // 返回 4
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63-
“循环数组”实现。
63+
**方法一:数组**
64+
65+
利用循环数组,实现循环双端队列。
66+
67+
基本元素有:
68+
69+
- front:队头元素的下标
70+
- size:队列中元素的个数
71+
- capacity:队列的容量
72+
- q:循环数组,存储队列中的元素
73+
74+
时间复杂度 $O(1)$,空间复杂度 $O(k)$。其中 $k$ 是队列的容量。
6475

6576
<!-- tabs:start -->
6677

@@ -265,6 +276,181 @@ class MyCircularDeque {
265276
*/
266277
```
267278

279+
### **C++**
280+
281+
```cpp
282+
class MyCircularDeque {
283+
public:
284+
vector<int> q;
285+
int front = 0;
286+
int size = 0;
287+
int capacity = 0;
288+
289+
MyCircularDeque(int k) {
290+
q.assign(k, 0);
291+
capacity = k;
292+
}
293+
294+
bool insertFront(int value) {
295+
if (isFull()) {
296+
return false;
297+
}
298+
if (!isEmpty()) {
299+
front = (front - 1 + capacity) % capacity;
300+
}
301+
q[front] = value;
302+
++size;
303+
return true;
304+
}
305+
306+
bool insertLast(int value) {
307+
if (isFull()) {
308+
return false;
309+
}
310+
int idx = (front + size) % capacity;
311+
q[idx] = value;
312+
++size;
313+
return true;
314+
}
315+
316+
bool deleteFront() {
317+
if (isEmpty()) {
318+
return false;
319+
}
320+
front = (front + 1) % capacity;
321+
--size;
322+
return true;
323+
}
324+
325+
bool deleteLast() {
326+
if (isEmpty()) {
327+
return false;
328+
}
329+
--size;
330+
return true;
331+
}
332+
333+
int getFront() {
334+
return isEmpty() ? -1 : q[front];
335+
}
336+
337+
int getRear() {
338+
return isEmpty() ? -1 : q[(front + size - 1) % capacity];
339+
}
340+
341+
bool isEmpty() {
342+
return size == 0;
343+
}
344+
345+
bool isFull() {
346+
return size == capacity;
347+
}
348+
};
349+
350+
/**
351+
* Your MyCircularDeque object will be instantiated and called as such:
352+
* MyCircularDeque* obj = new MyCircularDeque(k);
353+
* bool param_1 = obj->insertFront(value);
354+
* bool param_2 = obj->insertLast(value);
355+
* bool param_3 = obj->deleteFront();
356+
* bool param_4 = obj->deleteLast();
357+
* int param_5 = obj->getFront();
358+
* int param_6 = obj->getRear();
359+
* bool param_7 = obj->isEmpty();
360+
* bool param_8 = obj->isFull();
361+
*/
362+
```
363+
364+
### **Go**
365+
366+
```go
367+
type MyCircularDeque struct {
368+
q []int
369+
size int
370+
front int
371+
capacity int
372+
}
373+
374+
func Constructor(k int) MyCircularDeque {
375+
q := make([]int, k)
376+
return MyCircularDeque{q, 0, 0, k}
377+
}
378+
379+
func (this *MyCircularDeque) InsertFront(value int) bool {
380+
if this.IsFull() {
381+
return false
382+
}
383+
if !this.IsEmpty() {
384+
this.front = (this.front - 1 + this.capacity) % this.capacity
385+
}
386+
this.q[this.front] = value
387+
this.size++
388+
return true
389+
}
390+
391+
func (this *MyCircularDeque) InsertLast(value int) bool {
392+
if this.IsFull() {
393+
return false
394+
}
395+
idx := (this.front + this.size) % this.capacity
396+
this.q[idx] = value
397+
this.size++
398+
return true
399+
}
400+
401+
func (this *MyCircularDeque) DeleteFront() bool {
402+
if this.IsEmpty() {
403+
return false
404+
}
405+
this.front = (this.front + 1) % this.capacity
406+
this.size -= 1
407+
return true
408+
}
409+
410+
func (this *MyCircularDeque) DeleteLast() bool {
411+
if this.IsEmpty() {
412+
return false
413+
}
414+
this.size -= 1
415+
return true
416+
}
417+
418+
func (this *MyCircularDeque) GetFront() int {
419+
if this.IsEmpty() {
420+
return -1
421+
}
422+
return this.q[this.front]
423+
}
424+
425+
func (this *MyCircularDeque) GetRear() int {
426+
if this.IsEmpty() {
427+
return -1
428+
}
429+
return this.q[(this.front+this.size-1)%this.capacity]
430+
}
431+
432+
func (this *MyCircularDeque) IsEmpty() bool {
433+
return this.size == 0
434+
}
435+
436+
func (this *MyCircularDeque) IsFull() bool {
437+
return this.size == this.capacity
438+
}
439+
440+
/**
441+
* Your MyCircularDeque object will be instantiated and called as such:
442+
* obj := Constructor(k);
443+
* param_1 := obj.InsertFront(value);
444+
* param_2 := obj.InsertLast(value);
445+
* param_3 := obj.DeleteFront();
446+
* param_4 := obj.DeleteLast();
447+
* param_5 := obj.GetFront();
448+
* param_6 := obj.GetRear();
449+
* param_7 := obj.IsEmpty();
450+
* param_8 := obj.IsFull();
451+
*/
452+
```
453+
268454
### **...**
269455

270456
```

solution/0600-0699/0641.Design Circular Deque/README_EN.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,181 @@ class MyCircularDeque {
253253
*/
254254
```
255255

256+
### **C++**
257+
258+
```cpp
259+
class MyCircularDeque {
260+
public:
261+
vector<int> q;
262+
int front = 0;
263+
int size = 0;
264+
int capacity = 0;
265+
266+
MyCircularDeque(int k) {
267+
q.assign(k, 0);
268+
capacity = k;
269+
}
270+
271+
bool insertFront(int value) {
272+
if (isFull()) {
273+
return false;
274+
}
275+
if (!isEmpty()) {
276+
front = (front - 1 + capacity) % capacity;
277+
}
278+
q[front] = value;
279+
++size;
280+
return true;
281+
}
282+
283+
bool insertLast(int value) {
284+
if (isFull()) {
285+
return false;
286+
}
287+
int idx = (front + size) % capacity;
288+
q[idx] = value;
289+
++size;
290+
return true;
291+
}
292+
293+
bool deleteFront() {
294+
if (isEmpty()) {
295+
return false;
296+
}
297+
front = (front + 1) % capacity;
298+
--size;
299+
return true;
300+
}
301+
302+
bool deleteLast() {
303+
if (isEmpty()) {
304+
return false;
305+
}
306+
--size;
307+
return true;
308+
}
309+
310+
int getFront() {
311+
return isEmpty() ? -1 : q[front];
312+
}
313+
314+
int getRear() {
315+
return isEmpty() ? -1 : q[(front + size - 1) % capacity];
316+
}
317+
318+
bool isEmpty() {
319+
return size == 0;
320+
}
321+
322+
bool isFull() {
323+
return size == capacity;
324+
}
325+
};
326+
327+
/**
328+
* Your MyCircularDeque object will be instantiated and called as such:
329+
* MyCircularDeque* obj = new MyCircularDeque(k);
330+
* bool param_1 = obj->insertFront(value);
331+
* bool param_2 = obj->insertLast(value);
332+
* bool param_3 = obj->deleteFront();
333+
* bool param_4 = obj->deleteLast();
334+
* int param_5 = obj->getFront();
335+
* int param_6 = obj->getRear();
336+
* bool param_7 = obj->isEmpty();
337+
* bool param_8 = obj->isFull();
338+
*/
339+
```
340+
341+
### **Go**
342+
343+
```go
344+
type MyCircularDeque struct {
345+
q []int
346+
size int
347+
front int
348+
capacity int
349+
}
350+
351+
func Constructor(k int) MyCircularDeque {
352+
q := make([]int, k)
353+
return MyCircularDeque{q, 0, 0, k}
354+
}
355+
356+
func (this *MyCircularDeque) InsertFront(value int) bool {
357+
if this.IsFull() {
358+
return false
359+
}
360+
if !this.IsEmpty() {
361+
this.front = (this.front - 1 + this.capacity) % this.capacity
362+
}
363+
this.q[this.front] = value
364+
this.size++
365+
return true
366+
}
367+
368+
func (this *MyCircularDeque) InsertLast(value int) bool {
369+
if this.IsFull() {
370+
return false
371+
}
372+
idx := (this.front + this.size) % this.capacity
373+
this.q[idx] = value
374+
this.size++
375+
return true
376+
}
377+
378+
func (this *MyCircularDeque) DeleteFront() bool {
379+
if this.IsEmpty() {
380+
return false
381+
}
382+
this.front = (this.front + 1) % this.capacity
383+
this.size -= 1
384+
return true
385+
}
386+
387+
func (this *MyCircularDeque) DeleteLast() bool {
388+
if this.IsEmpty() {
389+
return false
390+
}
391+
this.size -= 1
392+
return true
393+
}
394+
395+
func (this *MyCircularDeque) GetFront() int {
396+
if this.IsEmpty() {
397+
return -1
398+
}
399+
return this.q[this.front]
400+
}
401+
402+
func (this *MyCircularDeque) GetRear() int {
403+
if this.IsEmpty() {
404+
return -1
405+
}
406+
return this.q[(this.front+this.size-1)%this.capacity]
407+
}
408+
409+
func (this *MyCircularDeque) IsEmpty() bool {
410+
return this.size == 0
411+
}
412+
413+
func (this *MyCircularDeque) IsFull() bool {
414+
return this.size == this.capacity
415+
}
416+
417+
/**
418+
* Your MyCircularDeque object will be instantiated and called as such:
419+
* obj := Constructor(k);
420+
* param_1 := obj.InsertFront(value);
421+
* param_2 := obj.InsertLast(value);
422+
* param_3 := obj.DeleteFront();
423+
* param_4 := obj.DeleteLast();
424+
* param_5 := obj.GetFront();
425+
* param_6 := obj.GetRear();
426+
* param_7 := obj.IsEmpty();
427+
* param_8 := obj.IsFull();
428+
*/
429+
```
430+
256431
### **...**
257432

258433
```

0 commit comments

Comments
 (0)