LeetCode.M641
题目:
题目大意:
模拟双端队列。空间上是循环的。
数据范围:
1 <= k <= 1000
0 <= value <= 1000
insertFront, insertLast, deleteFront, deleteLast, getFront, getRear, isEmpty, isFull 调用次数不大于 2000 次
一 、不推荐解法 :
思路:
这是空间上不循环的。
代码:
class MyCircularDeque {
int[] deque;
int maxlen, l, r;
public MyCircularDeque(int k) {
deque = new int[2020];
maxlen = k;
l = 1000;
r = 1000;
}
public boolean insertFront(int value) {
if (!isFull()){
deque[-- l] = value;
return true;
}
return false;
}
public boolean insertLast(int value) {
if (!isFull()){
deque[r ++ ] = value;
return true;
}
return false;
}
public boolean deleteFront() {
if (!isEmpty()){
l ++ ;
return true;
}
return false;
}
public boolean deleteLast() {
if (!isEmpty()){
r -- ;
return true;
}
return false;
}
public int getFront() {
if (!isEmpty()){
return deque[l];
}
return -1;
}
public int getRear() {
if (!isEmpty()){
return deque[r - 1];
}
return -1;
}
public boolean isEmpty() {
return l == r ? true : false;
}
public boolean isFull() {
return r - l == maxlen ? true : false;
}
}
时空复杂度分析等:
- 时间复杂度 : 每个操作都是O(1)
- 空间复杂度 : O(n)
二 、推荐解法 :
思路:
队头为l,队尾为r,l代表了队头元素所处的位置,r代表了队尾元素的下一个位置。为了将队空和队满判断条件分开,需空闲一个位置,所以初始化大小为capacity + 1大小的数组。所以操作为:
-
初始化:
public MyCircularDeque(int k) { l = 0; r = 0; capacity = k + 1; //容量加1,区分empty和full的判断条件 deque = new int[capacity]; }
-
判空:
public boolean isEmpty() { return l == r ? true : false; }
-
判满:
public boolean isFull() { return (r + 1) % capacity == l ? true : false; }
-
插入队头 :
public boolean insertFront(int value) { if (!isFull()){ l = (l - 1 + capacity) % capacity; deque[l] = value; return true; } return false; }
-
删除队头:
public boolean deleteFront() { if (!isEmpty()){ l = (l + 1) % capacity; return true; } return false; }
-
获得队头:
public int getFront() { if (!isEmpty()){ return deque[l]; } return -1; }
-
插入队尾 :
public boolean insertLast(int value) { if (!isFull()){ deque[r] = value; r = (r + 1) % capacity; return true; } return false; }
-
删除队尾:
public boolean deleteLast() { if (!isEmpty()){ r = (r - 1 + capacity) % capacity; return true; } return false; }
-
获得队尾:
public int getRear() { if (!isEmpty()){ return deque[(r - 1 + capacity) % capacity]; } return -1; }
代码:
class MyCircularDeque {
int[] deque;
int capacity, l, r;
public MyCircularDeque(int k) {
l = 0;
r = 0;
capacity = k + 1; //容量加1,区分empty和full的判断条件
deque = new int[capacity];
}
public boolean insertFront(int value) {
if (!isFull()){
l = (l - 1 + capacity) % capacity;
deque[l] = value;
return true;
}
return false;
}
public boolean insertLast(int value) {
if (!isFull()){
deque[r] = value;
r = (r + 1) % capacity;
return true;
}
return false;
}
public boolean deleteFront() {
if (!isEmpty()){
l = (l + 1) % capacity;
return true;
}
return false;
}
public boolean deleteLast() {
if (!isEmpty()){
r = (r - 1 + capacity) % capacity;
return true;
}
return false;
}
public int getFront() {
if (!isEmpty()){
return deque[l];
}
return -1;
}
public int getRear() {
if (!isEmpty()){
return deque[(r - 1 + capacity) % capacity];
}
return -1;
}
public boolean isEmpty() {
return l == r ? true : false;
}
public boolean isFull() {
return (r + 1) % capacity == l ? true : false;
}
}
时空复杂度分析等:
-
时间复杂度 : 每个操作都为O(1)
-
空间复杂度 : O(n)