設計你的循環隊列實現。 循環隊列是一種線性數據結構,其操作表現基於 FIFO(先進先出)原則並且隊尾被連接在隊首之后以形成一個循環。它也被稱為“環形緩沖器”。
循環隊列的一個好處是我們可以利用這個隊列之前用過的空間。在一個普通隊列里,一旦一個隊列滿了,我們就不能插入下一個元素,即使在隊列前面仍有空間。但是使用循環隊列,我們能使用這些空間去存儲新的值。
你的實現應該支持如下操作:
MyCircularQueue(k): 構造器,設置隊列長度為 k 。Front: 從隊首獲取元素。如果隊列為空,返回 -1 。Rear: 獲取隊尾元素。如果隊列為空,返回 -1 。enQueue(value): 向循環隊列插入一個元素。如果成功插入則返回真。deQueue(): 從循環隊列中刪除一個元素。如果成功刪除則返回真。isEmpty(): 檢查循環隊列是否為空。isFull(): 檢查循環隊列是否已滿。
示例:
MyCircularQueue circularQueue = new MycircularQueue(3); // 設置長度為 3 circularQueue.enQueue(1); // 返回 true circularQueue.enQueue(2); // 返回 true circularQueue.enQueue(3); // 返回 true circularQueue.enQueue(4); // 返回 false,隊列已滿 circularQueue.Rear(); // 返回 3 circularQueue.isFull(); // 返回 true circularQueue.deQueue(); // 返回 true circularQueue.enQueue(4); // 返回 true circularQueue.Rear(); // 返回 4
提示:
- 所有的值都在 0 至 1000 的范圍內;
- 操作數將在 1 至 1000 的范圍內;
- 請不要使用內置的隊列庫。
解題思路
1.使用雙指針記錄隊列元素位置,雙指針初始化為-1,刪除元素時驗證指針是否重合,是則初始化隊列
/**
* Initialize your data structure here. Set the size of the queue to be k.
* @param {number} k
*/
let MyCircularQueue = function (k) {
this.length = k
this.tail= -1
this.head = -1
this.queue= []
};
/**
* Insert an element into the circular queue. Return true if the operation is successful.
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function(value) {
if(this.isFull())return false
if(this.isEmpty())this.head = 0
this.tail = (this.tail+1)%this.length
this.queue[this.tail] = value
return true
};
/**
* Delete an element from the circular queue. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
if(!this.isEmpty()){
if(this.head===this.tail){
this.head = -1
this.tail = -1
}else{
this.head = (this.head+1)%this.length
}
return true
}
return false
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
if(!this.isEmpty())return this.queue[this.head]
return -1
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if(!this.isEmpty())return this.queue[this.tail]
return -1
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
return this.head===-1
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
return (this.tail+1)%this.length === this.head
};

2.使用雙指針記錄隊列元素位置,雙指針初始化為0,額外添加一個計數器count,刪除元素時驗證count是否為0,是則初始化隊列
let MyCircularQueue = function (k) {
this.length = k
this.tail= 0
this.head = 0
this.count = 0
this.queue= []
};
/**
* Insert an element into the circular queue. Return true if the operation is successful.
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function(value) {
if(this.isFull())return false
if(!this.isEmpty())this.tail = (this.tail+1)%this.length
this.queue[this.tail] = value
this.count++
return true
};
/**
* Delete an element from the circular queue. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
if(this.isEmpty()) return false
this.head = (this.head+1)%this.length
this.count --
if(this.isEmpty()){
this.head=0
this.tail=0
}
return true
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
if(!this.isEmpty())return this.queue[this.head]
return -1
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if(!this.isEmpty())return this.queue[this.tail]
return -1
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
return this.count === 0
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
return this.count === this.length
};

查看其它優秀答案的時候,發現使用ES6寫法的看起來比較整潔
貼在這里
class MyCircularQueue {
constructor(size) {
this.size = size;
this.queue = [];
}
enQueue(value) {
if (this.isFull()) return false;
this.queue.push(value);
return true;
}
deQueue() {
if (this.isEmpty()) return false;
this.queue.shift();
return true;
}
Front() {
if (this.isEmpty()) return -1;
return this.queue[0];
}
Rear() {
if (this.isEmpty()) return -1;
return this.queue[this.queue.length - 1];
}
isEmpty() {
return this.queue.length === 0;
}
isFull() {
return this.queue.length === this.size;
}
}
