- 设计循环双端队列
实现 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 次
方法一:数组
cpp
class MyCircularDeque {
private:
vector<int> elements;
int rear, front;
int capacity;
public:
MyCircularDeque(int k) {
capacity = k + 1;
rear = front = 0;
elements = vector<int>(k + 1);
}
bool insertFront(int value) {
if (isFull()) {
return false;
}
front = (front - 1 + capacity) % capacity;
elements[front] = value;
return true;
}
bool insertLast(int value) {
if (isFull()) {
return false;
}
elements[rear] = value;
rear = (rear + 1) % capacity;
return true;
}
bool deleteFront() {
if (isEmpty()) {
return false;
}
front = (front + 1) % capacity;
return true;
}
bool deleteLast() {
if (isEmpty()) {
return false;
}
rear = (rear - 1 + capacity) % capacity;
return true;
}
int getFront() {
if (isEmpty()) {
return -1;
}
return elements[front];
}
int getRear() {
if (isEmpty()) {
return -1;
}
return elements[(rear - 1 + capacity) % capacity];
}
bool isEmpty() {
return rear == front;
}
bool isFull() {
return (rear + 1) % capacity == front;
}
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque* obj = new MyCircularDeque(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/
方法二:链表
cpp
struct DLinkListNode {
int val;
DLinkListNode *prev, *next;
DLinkListNode(int _val): val(_val), prev(nullptr), next(nullptr) {
}
};
class MyCircularDeque {
private:
DLinkListNode *head, *tail;
int capacity;
int size;
public:
MyCircularDeque(int k): capacity(k), size(0), head(nullptr), tail(nullptr) {
}
bool insertFront(int value) {
if (size == capacity) {
return false;
}
DLinkListNode *node = new DLinkListNode(value);
if (size == 0) {
head = tail = node;
} else {
node->next = head;
head->prev = node;
head = node;
}
size++;
return true;
}
bool insertLast(int value) {
if (size == capacity) {
return false;
}
DLinkListNode *node = new DLinkListNode(value);
if (size == 0) {
head = tail = node;
} else {
tail->next = node;
node->prev = tail;
tail = node;
}
size++;
return true;
}
bool deleteFront() {
if (size == 0) {
return false;
}
DLinkListNode *node = head;
head = head->next;
if (head) {
head->prev = nullptr;
}
delete node;
size--;
return true;
}
bool deleteLast() {
if (size == 0) {
return false;
}
DLinkListNode *node = tail;
tail = tail->prev;
if (tail) {
tail->next = nullptr;
}
delete node;
size--;
return true;
}
int getFront() {
if (size == 0) {
return -1;
}
return head->val;
}
int getRear() {
if (size == 0) {
return -1;
}
return tail->val;
}
bool isEmpty() {
return size == 0;
}
bool isFull() {
return size == capacity;
}
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque* obj = new MyCircularDeque(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/