问题链接
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
。
示例
其实我觉得这道题示例没什么好看的。。。
输入
["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.链表实现
这道题其实比较简单,有几个点需要注意一下:
A. 空队列在做添加操作时,因为添加后只有一个元素,头尾指针都要指向它;
B. 只有一个元素的队列进行删除操作时,要注意将头/尾指针置null
。
2.数组实现
用数组来存放队列,用头、尾指针表示队列的位置。
代码示例(JAVA)
1.链表实现
class MyCircularDeque {
private Node head;
private Node tail;
private int curSize;
private int maxSize;
private static class Node {
private int val;
private Node next;
private Node pre;
public Node(int value) {
this.val = value;
this.next = null;
this.pre = null;
}
}
// 构造函数,双端队列最大为 k
public MyCircularDeque(int k) {
curSize = 0;
maxSize = k;
}
// 将一个元素添加到双端队列头部
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
Node newHead = new Node(value);
newHead.next = head;
if (head != null) {
head.pre = newHead;
}
head = newHead;
if (tail == null) {
tail = head;
}
curSize++;
return true;
}
// 将一个元素添加到双端队列尾部
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
Node newTail = new Node(value);
newTail.pre = tail;
if (tail != null) {
tail.next = newTail;
}
tail = newTail;
if (head == null) {
head = tail;
}
curSize++;
return true;
}
// 从双端队列头部删除一个元素
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
Node newHead = head.next;
if (newHead != null) {
newHead.pre = null;
head.next = null;
} else {
tail = null;
}
head = newHead;
curSize--;
return true;
}
// 从双端队列尾部删除一个元素
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
Node newTail = tail.pre;
if (newTail != null) {
tail.pre = null;
newTail.next = null;
} else {
head = null;
}
tail = newTail;
curSize--;
return true;
}
// 从双端队列头部获得一个元素。如果双端队列为空,返回 -1
public int getFront() {
return isEmpty() ? -1 : head.val;
}
// 获得双端队列的最后一个元素。 如果双端队列为空,返回 -1
public int getRear() {
return isEmpty() ? -1 : tail.val;
}
// 若双端队列为空,则返回 true ,否则返回 false
public boolean isEmpty() {
return curSize == 0;
}
// 若双端队列满了,则返回 true ,否则返回 false
public boolean isFull() {
return curSize == maxSize;
}
}
执行结果
![](https://img.haomeiwen.com/i13447148/f82b33d0346b20a4.png)
2.数组实现
class MyCircularDeque {
private int[] arr;
private int head;
private int tail;
public MyCircularDeque(int k) {
arr = new int[k];
head = -1;
tail = -1;
}
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
int newHead = (head - 1 + arr.length) % arr.length;
arr[newHead] = value;
head = newHead;
if (tail == -1) {
tail = newHead;
}
return true;
}
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
int newTail = (tail + 1) % arr.length;
arr[newTail] = value;
tail = newTail;
if (head == -1) {
head = newTail;
}
return true;
}
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
if (head == tail) {
head = -1;
tail = -1;
} else {
head = (head + 1) % arr.length;
}
return true;
}
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
if (head == tail) {
head = -1;
tail = -1;
} else {
tail = (tail - 1 + arr.length) % arr.length;
}
return true;
}
public int getFront() {
return head == -1 ? -1 : arr[head];
}
public int getRear() {
return head == -1 ? -1 : arr[tail];
}
public boolean isEmpty() {
return head == -1;
}
public boolean isFull() {
return (tail + 1) % arr.length == head;
}
}
执行结果
![](https://img.haomeiwen.com/i13447148/61a64d15fd1c7052.png)
网友评论