0641. 设计循环双端队列【中等】
1. 📝 题目描述
设计实现双端队列。
实现 MyCircularDeque 类:
MyCircularDeque(int k):构造函数,双端队列最大为k。boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回true,否则返回false。boolean insertLast():将一个元素添加到双端队列尾部。如果操作成功返回true,否则返回false。boolean deleteFront():从双端队列头部删除一个元素。 如果操作成功返回true,否则返回false。boolean deleteLast():从双端队列尾部删除一个元素。如果操作成功返回true,否则返回false。int getFront():从双端队列头部获得一个元素。如果双端队列为空,返回-1。int getRear():获得双端队列的最后一个元素。 如果双端队列为空,返回-1。boolean isEmpty():若双端队列为空,则返回true,否则返回false。boolean isFull():若双端队列满了,则返回true,否则返回false。
示例 1:
输入
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
输出
[null, true, true, true, false, 2, true, true, true, 4]
解释
MyCircularDeque circularDeque = new MycircularDeque(3); // 设置容量大小为3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已经满了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
提示:
1 <= k <= 10000 <= value <= 1000insertFront,insertLast,deleteFront,deleteLast,getFront,getRear,isEmpty,isFull调用次数不大于2000次
2. 🎯 s.1 - 循环数组
c
typedef struct {
int* arr;
int cap;
int front;
int rear;
} MyCircularDeque;
MyCircularDeque* myCircularDequeCreate(int k) {
MyCircularDeque* obj = (MyCircularDeque*)malloc(sizeof(MyCircularDeque));
obj->cap = k + 1;
obj->arr = (int*)malloc(sizeof(int) * obj->cap);
obj->front = 0;
obj->rear = 0;
return obj;
}
bool myCircularDequeInsertFront(MyCircularDeque* obj, int value) {
if ((obj->rear + 1) % obj->cap == obj->front) return false;
obj->front = (obj->front - 1 + obj->cap) % obj->cap;
obj->arr[obj->front] = value;
return true;
}
bool myCircularDequeInsertLast(MyCircularDeque* obj, int value) {
if ((obj->rear + 1) % obj->cap == obj->front) return false;
obj->arr[obj->rear] = value;
obj->rear = (obj->rear + 1) % obj->cap;
return true;
}
bool myCircularDequeDeleteFront(MyCircularDeque* obj) {
if (obj->front == obj->rear) return false;
obj->front = (obj->front + 1) % obj->cap;
return true;
}
bool myCircularDequeDeleteLast(MyCircularDeque* obj) {
if (obj->front == obj->rear) return false;
obj->rear = (obj->rear - 1 + obj->cap) % obj->cap;
return true;
}
int myCircularDequeGetFront(MyCircularDeque* obj) {
if (obj->front == obj->rear) return -1;
return obj->arr[obj->front];
}
int myCircularDequeGetRear(MyCircularDeque* obj) {
if (obj->front == obj->rear) return -1;
return obj->arr[(obj->rear - 1 + obj->cap) % obj->cap];
}
bool myCircularDequeIsEmpty(MyCircularDeque* obj) {
return obj->front == obj->rear;
}
bool myCircularDequeIsFull(MyCircularDeque* obj) {
return (obj->rear + 1) % obj->cap == obj->front;
}
void myCircularDequeFree(MyCircularDeque* obj) {
free(obj->arr);
free(obj);
}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
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
js
/**
* @param {number} k
*/
var MyCircularDeque = function (k) {
this.cap = k + 1
this.arr = new Array(this.cap)
this.front = 0
this.rear = 0
}
MyCircularDeque.prototype.insertFront = function (value) {
if (this.isFull()) return false
this.front = (this.front - 1 + this.cap) % this.cap
this.arr[this.front] = value
return true
}
MyCircularDeque.prototype.insertLast = function (value) {
if (this.isFull()) return false
this.arr[this.rear] = value
this.rear = (this.rear + 1) % this.cap
return true
}
MyCircularDeque.prototype.deleteFront = function () {
if (this.isEmpty()) return false
this.front = (this.front + 1) % this.cap
return true
}
MyCircularDeque.prototype.deleteLast = function () {
if (this.isEmpty()) return false
this.rear = (this.rear - 1 + this.cap) % this.cap
return true
}
MyCircularDeque.prototype.getFront = function () {
if (this.isEmpty()) return -1
return this.arr[this.front]
}
MyCircularDeque.prototype.getRear = function () {
if (this.isEmpty()) return -1
return this.arr[(this.rear - 1 + this.cap) % this.cap]
}
MyCircularDeque.prototype.isEmpty = function () {
return this.front === this.rear
}
MyCircularDeque.prototype.isFull = function () {
return (this.rear + 1) % this.cap === this.front
}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
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
py
class MyCircularDeque:
def __init__(self, k: int):
self.cap = k + 1
self.arr = [0] * self.cap
self.front = 0
self.rear = 0
def insertFront(self, value: int) -> bool:
if self.isFull():
return False
self.front = (self.front - 1) % self.cap
self.arr[self.front] = value
return True
def insertLast(self, value: int) -> bool:
if self.isFull():
return False
self.arr[self.rear] = value
self.rear = (self.rear + 1) % self.cap
return True
def deleteFront(self) -> bool:
if self.isEmpty():
return False
self.front = (self.front + 1) % self.cap
return True
def deleteLast(self) -> bool:
if self.isEmpty():
return False
self.rear = (self.rear - 1) % self.cap
return True
def getFront(self) -> int:
return -1 if self.isEmpty() else self.arr[self.front]
def getRear(self) -> int:
return -1 if self.isEmpty() else self.arr[(self.rear - 1) % self.cap]
def isEmpty(self) -> bool:
return self.front == self.rear
def isFull(self) -> bool:
return (self.rear + 1) % self.cap == self.front1
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
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
- 时间复杂度:
,所有操作均为常数时间 - 空间复杂度:
算法思路:
- 用大小为
k + 1的循环数组,维护front和rear两个指针 - 空判断:
front == rear;满判断:(rear + 1) % cap == front - 头部插入向前移动
front,尾部插入向后移动rear