Time:2019/4/15
Title: Design Circular Deque
Difficulty: Medium
Author: 小鹿
问题:Design Circular Deque
Design your implementation of the circular double-ended queue (deque).
Your implementation should support following operations:
-
MyCircularDeque(k)
: Constructor, set the size of the deque to be k. -
insertFront()
: Adds an item at the front of Deque. Return true if the operation is successful. -
insertLast()
: Adds an item at the rear of Deque. Return true if the operation is successful. -
deleteFront()
: Deletes an item from the front of Deque. Return true if the operation is successful. -
deleteLast()
: Deletes an item from the rear of Deque. Return true if the operation is successful. -
getFront()
: Gets the front item from the Deque. If the deque is empty, return -1. -
getRear()
: Gets the last item from Deque. If the deque is empty, return -1. -
isEmpty()
: Checks whether Deque is empty or not. -
isFull()
: Checks whether Deque is full or not.
设想完成双端行列。
你的完成须要支撑以下操纵:
- MyCircularDeque(k):组织函数,双端行列的大小为k。
- insertFront():将一个元素添加到双端行列头部。 假如操纵胜利返回 true。
- insertLast():将一个元素添加到双端行列尾部。假如操纵胜利返回 true。
- deleteFront():从双端行列头部删除一个元素。 假如操纵胜利返回 true。
- deleteLast():从双端行列尾部删除一个元素。假如操纵胜利返回 true。
- getFront():从双端行列头部取得一个元素。假如双端行列为空,返回 -1。
- getRear():取得双端行列的末了一个元素。 假如双端行列为空,返回 -1。
- isEmpty():搜检双端行列是不是为空。
- isFull():搜检双端行列是不是满了。
Example:
MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1); // return true
circularDeque.insertLast(2); // return true
circularDeque.insertFront(3); // return true
circularDeque.insertFront(4); // return false, the queue is full
circularDeque.getRear(); // return 2
circularDeque.isFull(); // return true
circularDeque.deleteLast(); // return true
circularDeque.insertFront(4); // return true
circularDeque.getFront(); // return 4
Note:
- All values will be in the range of [0, 1000].
- The number of operations will be in the range of [1, 1000].
- Please do not use the built-in Deque library.
Solve:
▉ 算法思绪
借助 Javascript 中数组中的 API 可疾速完成一个双向行列。如:
arr.pop()
: 删除数组尾部末了一个数据。arr.push()
:在数组尾部插进去一个数据。arr.shift()
:数组头部删除第一个数据。arr.unshift()
:数组头部插进去一个数据。以上数组供应的 API 使得更轻易的对数组举行操纵和模仿其他数据结构的操纵,栈、行列等。
▉ 代码完成
//双端列表组织
var MyCircularDeque = function(k) {
this.deque = [];
this.size = k;
};
/**
* Adds an item at the front of Deque. Return true if the operation is successful.
* @param {number} value
* @return {boolean}
* 功用:行列头部入队
*/
MyCircularDeque.prototype.insertFront = function(value) {
if(this.deque.length === this.size){
return false;
}else{
this.deque.unshift(value);
return true;
}
};
/**
* Adds an item at the rear of Deque. Return true if the operation is successful.
* @param {number} value
* @return {boolean}
* 功用:行列尾部入队
*/
MyCircularDeque.prototype.insertLast = function(value) {
if(this.deque.length === this.size){
return false;
}else{
this.deque.push(value);
return true;
}
};
/**
* Deletes an item from the front of Deque. Return true if the operation is successful.
* @return {boolean}
* 功用:行列头部出队
*/
MyCircularDeque.prototype.deleteFront = function() {
if(this.deque.length === 0){
return false;
}else{
this.deque.shift();
return true;
}
};
/**
* Deletes an item from the rear of Deque. Return true if the operation is successful.
* @return {boolean}
* 功用:行列尾部出队
*/
MyCircularDeque.prototype.deleteLast = function() {
if(this.deque.length === 0){
return false;
}else{
this.deque.pop();
return true;
}
};
/**
* Get the front item from the deque.
* @return {number}
* 功用:猎取行列头部第一个数据
*/
MyCircularDeque.prototype.getFront = function() {
if(this.deque.length === 0){
return -1;
}else{
return this.deque[0];
}
};
/**
* Get the last item from the deque.
* @return {number}
* 功用:猎取行列尾部第一个数据
*/
MyCircularDeque.prototype.getRear = function() {
if(this.deque.length === 0){
return -1;
}else{
return this.deque[this.deque.length - 1];
}
};
/**
* Checks whether the circular deque is empty or not.
* @return {boolean}
* 功用:推断双端行列是不是为空
*/
MyCircularDeque.prototype.isEmpty = function() {
if(this.deque.length === 0){
return true;
}else{
return false;
}
};
/**
* Checks whether the circular deque is full or not.
* @return {boolean}
* 功用:推断双端行列是不是为满
*/
MyCircularDeque.prototype.isFull = function() {
if(this.deque.length === this.size){
return true;
}else{
return false;
}
};
//测试
var obj = new MyCircularDeque(3)
var param_1 = obj.insertFront(1)
var param_2 = obj.insertLast(2)
console.log('-----------------------------插进去数据------------------------')
console.log(`${param_1}${param_2}`)
var param_3 = obj.deleteFront()
var param_4 = obj.deleteLast()
console.log('-----------------------------删除数据------------------------')
console.log(`${param_3}${param_4}`)
var param_5 = obj.getFront()
var param_6 = obj.getRear()
console.log('-----------------------------猎取数据------------------------')
console.log(`${param_5}${param_6}`)
var param_7 = obj.isEmpty()
var param_8 = obj.isFull()
console.log('-----------------------------推断空/满------------------------')
console.log(`${param_7}${param_8}`)
迎接一同加入到 LeetCode 开源 Github 堆栈,能够向 me 提交您其他言语的代码。在堆栈上对峙和小伙伴们一同打卡,配合完美我们的开源小堆栈!
Github:
https://github.com/luxiangqia…
迎接关注我个人民众号:「一个不甘寻常的码农」,记录了本身一起自学编程的故事。