0%

『LeetCode』641 设计循环双端队列

题目

641. 设计循环双端队列

设计实现双端队列。

实现 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();// 返回 4

提示:

  • 1 <= k <= 1000
  • 0 <= value <= 1000
  • insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000

标签

设计, 队列, 数组, 链表

相似题目


题解

【设计循环双端队列】基本数据结构

双端队列

双端队列是队列和栈的进阶版,也是基本的数据结构,循环队列是其常见的实现方式,需要注意,如何判断队列空和队列满,常见的方法有两种:

  • 留一个空位,队头队尾指针重合为空,队头指针在队尾指针前面一个位置为满
  • 用一个变量记录队列中元素数量

实现上其实第二种方法稍微简单一丢丢,第一种方法代码格式更简洁统一一丢丢。

除此之外,注意一下指针所指向的是元素还是元素的下一个位置。

下面代码中, 用一个变量记录队列中元素数量,指针指向的均为元素的下一个位置

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
# Code language: Python
class MyCircularDeque:

def __init__(self, k: int):
self.st = [0] * k
self.front, self.tail = 1, 0
self.size, self.length = 0, k

def insertFront(self, value: int) -> bool:
if self.isFull():
return False
self.st[self.front] = value
self.front = (self.front + 1) % self.length
self.size += 1
return True

def insertLast(self, value: int) -> bool:
if self.isFull():
return False
self.st[self.tail] = value
self.tail = (self.tail + self.length - 1) % self.length
self.size += 1
return True

def deleteFront(self) -> bool:
if self.isEmpty():
return False
self.front = (self.front + self.length - 1) % self.length
self.size -= 1
return True

def deleteLast(self) -> bool:
if self.isEmpty():
return False
self.tail = (self.tail + 1) % self.length
self.size -= 1
return True

def getFront(self) -> int:
if self.isEmpty():
return -1
return self.st[(self.front + self.length - 1) % self.length]

def getRear(self) -> int:
if self.isEmpty():
return -1
return self.st[(self.tail + 1) % self.length]

def isEmpty(self) -> bool:
return self.size == 0

def isFull(self) -> bool:
return self.size == self.length
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
// Code language: Java
class MyCircularDeque {

private int[] st;
int front, tail, size, length;

public MyCircularDeque(int k) {
st = new int[k];
front = 1; tail = 0;
size = 0; length = k;
}

public boolean insertFront(int value) {
if (isFull()) return false;
st[front] = value;
front = (front + 1) % length;
size++;
return true;
}

public boolean insertLast(int value) {
if (isFull()) return false;
st[tail] = value;
tail = (tail - 1 + length) % length;
size++;
return true;
}

public boolean deleteFront() {
if (isEmpty()) return false;
front = (front - 1 + length) % length;
size--;
return true;
}

public boolean deleteLast() {
if (isEmpty()) return false;
tail = (tail + 1) % length;
size--;
return true;
}

public int getFront() {
if (isEmpty()) return -1;
return st[(front - 1 + length) % length];
}

public int getRear() {
if (isEmpty()) return -1;
return st[(tail + 1) % length];
}

public boolean isEmpty() {
return size == 0;
}

public boolean isFull() {
return size == length;
}
}
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
// Code language: C++
class MyCircularDeque {
private:
vector<int> st;
int front, tail, size, length;
public:
MyCircularDeque(int k):front(1), tail(0), size(0), length(k) {
st.resize(k);
}

bool insertFront(int value) {
if (isFull()) return false;
st[front] = value;
front = (front + 1) % length;
size++;
return true;
}

bool insertLast(int value) {
if (isFull()) return false;
st[tail] = value;
tail = (tail - 1 + length) % length;
size++;
return true;
}

bool deleteFront() {
if (isEmpty()) return false;
front = (front - 1 + length) % length;
size--;
return true;
}

bool deleteLast() {
if (isEmpty()) return false;
tail = (tail + 1) % length;
size--;
return true;
}

int getFront() {
if (isEmpty()) return -1;
return st[(front - 1 + length) % length];
}

int getRear() {
if (isEmpty()) return -1;
return st[(tail + 1) % length];
}

bool isEmpty() {
return size == 0;
}

bool isFull() {
return size == length;
}
};
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
/* Code language: JavaScript */
/**
* @param {number} k
*/
var MyCircularDeque = function(k) {
this.length = k;this.size = 0;
this.front= 1; this.tail = 0;
this.st = new Array(k).fill(0);
};

/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertFront = function(value) {
if (this.isFull())
return false;
this.st[this.front] = value;
this.front = (this.front + 1) % this.length;
this.size += 1;
return true;
};

/**
* @param {number} value
* @return {boolean}
*/
MyCircularDeque.prototype.insertLast = function(value) {
if (this.isFull())
return false;
this.st[this.tail] = value;
this.tail = (this.tail + this.length - 1) % this.length;
this.size += 1;
return true;
};

/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteFront = function() {
if (this.isEmpty())
return false;
this.front = (this.front + this.length - 1) % this.length;
this.size -= 1;
return true;
};

/**
* @return {boolean}
*/
MyCircularDeque.prototype.deleteLast = function() {
if (this.isEmpty())
return false;
this.tail = (this.tail + 1) % this.length;
this.size -= 1;
return true;
};

/**
* @return {number}
*/
MyCircularDeque.prototype.getFront = function() {
if (this.isEmpty())
return -1;
return this.st[(this.front + this.length - 1) % this.length];
};

/**
* @return {number}
*/
MyCircularDeque.prototype.getRear = function() {
if (this.isEmpty())
return -1;
return this.st[(this.tail + 1) % this.length];
};

/**
* @return {boolean}
*/
MyCircularDeque.prototype.isEmpty = function() {
return this.size == 0;
};

/**
* @return {boolean}
*/
MyCircularDeque.prototype.isFull = function() {
return this.size == this.length;
};
  • 时间复杂度: 所有操作均为 \(O(1)\)
  • 空间复杂度: \(O(k)\)

如果对你有帮助的话,请给我点个赞吧~

欢迎前往 我的博客Algorithm - Github 查看更多题解

--- ♥ end ♥ ---

欢迎关注我呀~