「这是我参与2022首次更文挑战的第1天,活动详情查看:2022首次更文挑战」。
1670. 设计前中后队列
整体思路是: 对一个队列/数组,在第一位新增/删除一个新的元素,在最后位置新增/删除一个元素,在中间位置新增/删除一个元素。
设计一个队列,需要支持在前,中,后三个位置的 push 和 pop 操作。
完成 FrontMiddleBack 类,有以下方法:
- FrontMiddleBack() 初始化队列。
- void pushFront(int val) 将
val添加到队列的 最前面 。 - void pushMiddle(int val) 将
val添加到队列的 正中间 。 - void pushBack(int val) 将
val添加到队里的 最后面 。 - int popFront() 将最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
- int popMiddle() 将正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
- int popBack() 将最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
需要注意的是当有 两个 中间位置的时候,选择靠前面的位置进行操作。比如说:
- 将 6 添加到 [1, 2, 3, 4, 5] 的中间位置,结果数组为 [1, 2, 6, 3, 4, 5] 。
- 从 [1, 2, 3, 4, 5, 6] 的中间位置弹出元素,返回 3 ,数组变为 [1, 2, 4, 5, 6] 。
代码实现:
/*
* @lc app=leetcode.cn id=1670 lang=javascript
*
* [1670] 设计前中后队列
*/
// @lc code=start
/**
* @description 初始化队列
*/
var FrontMiddleBackQueue = function() {
this.leftArray = [];
this.rightArray = [];
};
/**
* @description 将 val 添加到队列的 最前面
* @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())
}
};
/**
* @description 将 val 添加到队列的 正中间
* @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);
}
};
/**
* @description 将 val 添加到队里的 最后面
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushBack = function(val) {
this.rightArray.push(val)
if (this.rightArray.length == this.leftArray.length + 2) {
this.leftArray.push(this.rightArray.shift())
}
};
/**
* @description 将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1
* @return {number}
*/
FrontMiddleBackQueue.prototype.popFront = function () {
// 如果队列中无数据返回-1
if (!this.rightArray.length) return -1;
// 如果leftArray的长度为0,则返回rightArray的第一个元素
if (!this.leftArray.length) return this.rightArray.shift()
// 至少有2个元素
let ret = this.leftArray.shift()
if (this.leftArray.length + 1 < this.rightArray.length) {
// 失衡了,需要调整,因为右边太长了,需要把右边吐出一个放到左侧
this.leftArray.push(this.rightArray.shift())
}
return ret
};
/**
* @description 将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1
* @return {number}
*/
FrontMiddleBackQueue.prototype.popMiddle = function() {
if (!this.rightArray.length) return -1;
if (!this.leftArray.length) return this.rightArray.shift()
if (this.rightArray.length == this.leftArray.length) return this.leftArray.pop()
return this.rightArray.shift()
};
/**
* @description 将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1
* @return {number}
*/
FrontMiddleBackQueue.prototype.popBack = function() {
if (!this.rightArray.length) return -1;
if (!this.leftArray.length) return this.rightArray.shift()
let ret = this.rightArray.pop()
if (this.rightArray.length < this.leftArray.length) {
this.rightArray.unshift(this.leftArray.pop())
}
return ret
};
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* var obj = new FrontMiddleBackQueue()
* obj.pushFront(val)
* obj.pushMiddle(val)
* obj.pushBack(val)
* var param_4 = obj.popFront()
* var param_5 = obj.popMiddle()
* var param_6 = obj.popBack()
*/
// @lc code=end