题目描述
思路
双数组模拟
可以利用循环队列实现双端队列,在循环队列的基础上,增加insertFront和deleteFront函数实现即可。队列具备如下几个属性:front(队首索引)、rear(队尾索引)、elements(一个固定大小的数组)、capacity(循环队列的容量)。根据题意,循环双端队列需要实现如下接口:
- MyCircularDeque(int k):初始化队列,同时数组空间初始化大小为k+1,front和rear都初始化为0。
- insertFront(int value):当队列未满时,在队首插入一个元素,将队首指针front移动一个位置,更新队首索引为(front-1+capacity) mod capacity。
- insertLast(int value):当队列未满时,在队尾插入一个元素,将队尾指针rear更新为(rear+1) mod capacity。
- deleteFront():当队列不为空时,从队首删除一个元素,并同时将队首的索引front更新为(front+1) mod capacity。
- deleteLast():当队列不为空时,从队尾删除一个元素,并同时将队尾索引rear更新为(rear-1+capacity) mod capacity。
- getFront():当队列非空,返回队首元素。
- getRear():当队列非空,返回队尾元素。
- isEmpty():判断队列是否为空,只需要判断rear和front是否相等。
- isFull():判断队列是否已满,只需判断front是否等于(rear+1) mod capacity。
Python实现
class MyCircularDeque:
def __init__(self, k: int):
self.front = self.rear = 0
self.elements = [0] * (k+1)
def insertFront(self, value: int) -> bool:
if self.isFull():
return False
self.front = (self.front - 1) % len(self.elements)
self.elements[self.front] = value
return True
def insertLast(self, value: int) -> bool:
if self.isFull():
return False
self.elements[self.rear] = value
self.rear = (self.rear + 1) % len(self.elements)
return True
def deleteFront(self) -> bool:
if self.isEmpty():
return False
self.front = (self.front + 1) % len(self.elements)
return True
def deleteLast(self) -> bool:
if self.isEmpty():
return False
self.rear = (self.rear - 1) % len(self.elements)
return True
def getFront(self) -> int:
return -1 if self.isEmpty() else self.elements[self.front]
def getRear(self) -> int:
return -1 if self.isEmpty() else self.elements[(self.rear - 1) % len(self.elements)]
def isEmpty(self) -> bool:
return self.front == self.rear
def isFull(self) -> bool:
return (self.rear + 1) % len(self.elements) == self.front
# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()
Java实现
class MyCircularDeque {
private int[] elements;
private int front, rear, capacity;
public MyCircularDeque(int k) {
capacity = k + 1;
rear = front = 0;
elements = new int[k+1];
}
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
front = (front - 1 + capacity) % capacity;
elements[front] = value;
return true;
}
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
elements[rear] = value;
rear = (rear + 1) % capacity;
return true;
}
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
front = (front + 1) % capacity;
return true;
}
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
rear = (rear - 1 + capacity) % capacity;
return true;
}
public int getFront() {
if (isEmpty()) {
return -1;
}
return elements[front];
}
public int getRear() {
if (isEmpty()) {
return -1;
}
return elements[(rear - 1 + capacity) % capacity];
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return (rear + 1) % capacity == front;
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* boolean param_1 = obj.insertFront(value);
* boolean param_2 = obj.insertLast(value);
* boolean param_3 = obj.deleteFront();
* boolean param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* boolean param_7 = obj.isEmpty();
* boolean param_8 = obj.isFull();
*/
C++实现
class MyCircularDeque {
private:
vector<int> elements;
int front, rear, capacity;
public:
MyCircularDeque(int k) {
capacity = k+1;
front = rear = 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 front == rear;
}
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();
*/