携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第18天,点击查看活动详情
题目详情
LeetCode题库序号 641. 设计循环双端队列 ,难度为 中等。
Tag : 「循环数组」
设计实现双端队列。
实现 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 次
循环数组
题解思路:这道题目需要使用到循环数组的方式去实现循环双端队列,这道题目比较复杂的地方有以下几处,首先是判断队列为空和为满的条件,我们将头节点和尾节点相同的时候就判断队列是空的,判断为满的时候,我们这里需要冗余一个索引位,为满的公式就是 (tail + 1) % capacity == head,后续的数据插入和数据删除的节点变化,也是按照以下的规律来进行的。详情见以下代码:
题解代码
/**
* MyCircularDeque操作类
*
* @author JackPan
* @date 2022/08/15 12:40
**/
public class MyCircularDeque {
private int[] array;
private int head;
private int tail;
private int capacity = 0;
public MyCircularDeque(int k) {
capacity = k + 1;
array = new int[capacity];
head = 0;
tail = 0;
}
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
head = (head - 1 + capacity) % capacity;
array[head] = value;
return true;
}
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
array[tail] = value;
tail = (tail + 1) % capacity;
return true;
}
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
head = (head + 1) % capacity;
return true;
}
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
tail = (tail - 1 + capacity) % capacity;
return true;
}
public int getFront() {
if (isEmpty()) {
return -1;
}
return array[head];
}
public int getRear() {
if (isEmpty()) {
return -1;
}
return array[(tail - 1 + capacity) % capacity];
}
public boolean isEmpty() {
return head == tail;
}
public boolean isFull() {
return (tail + 1) % capacity == head;
}
}
/**
* 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();
*/
结尾
我的"刷完LeetCode题库"系列文章的第 No.641 序号的题目,本次刷题之旅系列开始于 2022-06-12,因为LeetCode上部分是有锁题,我自己的目标是将先把所有不带锁的题目刷完。自己能够通过这次刷题之旅勉励自己,并且提升逻辑思维能力。这个系列的文章就是会见证我自己的一个成长过程!
思路虽然不是最优的,但是我会尽我所能!
为了让我自己的刷题之旅不中断,我特地建立了相关的仓库,来记录我自己的刷题之旅。 github.com/jackpan123/… 。