题目:
设计实现双端队列。
实现 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(); // 返回
提示:
1 <= k <= 1000
0 <= value <= 1000
insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-circular-deque
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
数组用于模拟固定容量的循环队列是非常合适的且便捷的。
针对容量为k的双端队列,我们申请一个容量为k+1的数组。
用整型front作为数组的下标,指向队列的前端;用整型rear作为数组的下标,指向队列的尾部+1的位置,即下一个插入的位置。
队列为空时front == rear; 队列为满时,(rear+1)%capacity == front;之所以用k+1作为数组的容量,就是为了防止rear和front相等时即可能满也可能空;不用一个额外的变量进行判断。
往前插入时,front--,并对capacity取模。往后插入时,rear++,并对capacity取模。这样越界的时候就重新回到了数组的另一端;这也是循环队列中循环的意思。
代码:
class MyCircularDeque {
public:
int rear = 0;
int front = 0;
int capacity = 0;
vector<int> queue;
/** Initialize your data structure here. Set the size of the deque to be k. */
MyCircularDeque(int k) {
capacity = k+1;
queue = vector<int>(k+1, 0);
}
void show() {
for (int i = front; i != rear; i++) {
i %= capacity;
if (i == rear) break;
cout << queue[i] << " ";
}
cout << endl;
}
/** Adds an item at the front of Deque. Return true if the operation is successful. */
bool insertFront(int value) {
if (isFull()) return false;
front--;
front += capacity;
front %= capacity;
queue[front] = value;
return true;
}
/** Adds an item at the rear of Deque. Return true if the operation is successful. */
bool insertLast(int value) {
if (isFull()) return false;
queue[rear] = value;
rear++;
rear %= capacity;
return true;
}
/** Deletes an item from the front of Deque. Return true if the operation is successful. */
bool deleteFront() {
if (isEmpty()) return false;
front++;
front %= capacity;
return true;
}
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
bool deleteLast() {
if (isEmpty()) return false;
rear--;
rear += capacity;
rear %= capacity;
return true;
}
/** Get the front item from the deque. */
int getFront() {
if (isEmpty()) return -1;
return queue[front];
}
/** Get the last item from the deque. */
int getRear() {
if (isEmpty()) return -1;
return queue[(rear-1+capacity)%capacity];
}
/** Checks whether the circular deque is empty or not. */
bool isEmpty() {
return front == rear;
}
/** Checks whether the circular deque is full or not. */
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();
*/