题目描述
请你设计一个队列,支持在前,中,后三个位置的 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] 。
示例 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 <= 最多调用 1000 次 pushFront, pushMiddle, pushBack, popFront, popMiddle 和 popBack 。
来源:力扣(LeetCode) 链接:leetcode-cn.com/problems/de… 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解思路
该题主要的难点在于如何找到题目所述的中间位置,我们可以将队列差分为二,用左右两个队列组成完整队列的方式来表达这个队列。
- 当队列长度为奇数,保证左队列比右队列长度少1,那么中间值即为右队列首位。
- 当队列长度为偶数时,那么中间值即为左队列末位。 于是我们利用数组的shift,unshift,pop,push四个方法即可完成相应的设计。
题解代码
* @lc app=leetcode.cn id=1670 lang=javascript
*
* [1670] 设计前中后队列
*/
// @lc code=start
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){
return this.rightArray.unshift(val);
}else{
this.leftArray.push(val);
}
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushBack = function(val) {
this.rightArray.push(val);//将数添加到右队列末位
//如果添加完成后右队列长度比左队列长2
//则将右队列首位移出添加到左队列末位
if(this.leftArray.length + 2 == this.rightArray.length){
this.leftArray.push(this.rightArray.shift());
}
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popFront = function() {
//空队列返回-1
if(!this.rightArray.length) return -1;
//只有一个数的队列,数必在右队列返回右队列的数
if(!this.leftArray.length) return this.rightArray.shift();
//取出左队列首位
let ret = this.leftArray.shift();
//左队列移除数据后,在此判断右队列长度是否比左队列长2
//如果长2就调整右队列首位到左队列末位
if(this.leftArray.length + 2 == this.rightArray.length){
this.leftArray.push(this.rightArray.shift());
}
return ret;
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popMiddle = function() {
//空队列返回-1
if(!this.rightArray.length) return -1;
//只有一个数的队列,数必在右队列返回右队列的数
if(!this.leftArray.length) return this.rightArray.shift();
//左右队列长度相等,中间数为左队列末位数
//右队列长,则中间数为右队列首位
if(this.leftArray.length == this.rightArray.length){
return this.leftArray.pop();
}else{
return this.rightArray.shift();
}
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popBack = function() {
//空队列返回-1
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;
};