「这是我参与11月更文挑战的第 11 天,活动详情查看:2021最后一次更文挑战」
题目
题目来源:1670. 设计前中后队列
请你设计一个队列,支持在前,中,后三个位置的 push 和 pop 操作。
请你完成 FrontMiddleBack 类:
tMiddleBack() 初始化队列。
void pushFront(int val) 将 val 添加到队列的 最前面 。
void pushMiddle(int val) 将 val 添加到队列的 正中间 。
void pushBack(int val) 将 val 添加到队里的 最后面 。v int popFront() 将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
int popMiddle() 将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 >-1 。
int popBack() 将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
请注意当有 两个 中间位置的时候,选择靠前面的位置进行操作。比方说:
加到 [1, 2, 3, 4, 5] 的中间位置,结果数组为 [1, 2, 6, 3, 4, 5] 。 从 [1, 2, 3, 4, 5, 6] 的中间位置弹出元素,返回 3 ,数组变为 [1, 2, 4, 5, 6] 。
示例 1:
输入: ["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"] [[], [1], [2], [3], [4], [], [], [], [], []]
输出:
[null, null, null, null, null, 1, 3, 4, 2, -1]
解释:
FrontMiddleBackQueue q = new FrontMiddleBackQueue();
q.pushFront(1); // [1]
q.pushBack(2); // [1, 2]
q.pushMiddle(3); // [1, 3, 2]
q.pushMiddle(4); // [1, 4, 3, 2]
q.popFront(); // 返回 1 -> [4, 3, 2]
q.popMiddle(); // 返回 3 -> [4, 2]
q.popMiddle(); // 返回 4 -> [2]
q.popBack(); // 返回 2 -> []
q.popFront(); // 返回 -1 -> [] (队列为空)
提示:
1 <= val <= 109 最多调用 1000 次 pushFront, pushMiddle, pushBack, popFront, popMiddle 和 popBack 。
分析
-
命名俩个队列,进行添加操作后,中间值再右队列中
-
将6添加到队列最前面,将左队列unshift,将元素6添加到队列最前面
-
添加完左队列长度大于右队列
-
将左队列pop删除最后一位,右队列unshift刚刚删除的
-
将6添加到队列中间
-
左队列长度等于有队列,将左队列的最后一位pop删除
-
删除后的unshift右队列
-
将6push到左队列
-
将6添加到队列最后面
-
最中间数再右队列中,并且左队列小于右队列
-
否则删除右队列第一个,添加到左队列尾部
-
删除最前面的
-
判断左队列是否为空,为空返回-1,否则shift左队列
-
如果左队列长度大于有队列,pop左队列,将删除的unshift右队列
-
删除中间元素
-
判断左队列是否为空,为空返回-1,否则pop左队列
-
继续判断左队列长度小于右队列,不符合则,pop左队列,将删除的unshift右队列
-
删除最后元素
-
判断右队列是否为空,为空返回-1,否则,pop右队列
-
如果左队列长度大于右队列,则pop左队列,将删除的unshift右队列
代码实现
var FrontMiddleBackQueue = function() {
this.leftArray=[]
this.rightArray = []
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushFront = function(val) {
this.leftArray.unshift(val)
if(this.leftArray.length > this.rightArray.length) this.rightArray.unshift(this.leftArray.pop())
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushMiddle = function(val) {
if(this.leftArray.length == this.rightArray.length) this.rightArray.unshift(val)
else this.leftArray.push(val)
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushBack = function(val) {
this.rightArray.push(val)
if(this.leftArray.length + 1 < this.rightArray.length) this.leftArray.push(this.rightArray.shift())
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popFront = function() {
if(!this.rightArray.length) return -1
if(!this.leftArray.length) return this.rightArray.shift()
let front = this.leftArray.shift()
if(this.leftArray.length + 1 < this.rightArray.length) this.leftArray.push(this.rightArray.shift())
return front
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popMiddle = function() {
if(!this.rightArray.length){
return -1
}
if(this.leftArray.length == this.rightArray.length){
return this.leftArray.pop()
}
return this.rightArray.shift()
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popBack = function() {
if(!this.rightArray.length) return -1
let back = this.rightArray.pop()
if(this.leftArray.length > this.rightArray.length) this.rightArray.unshift(this.leftArray.pop())
return back
};