原理: 先进先出, FIFO, first in first out。 普通队列, 环形队列。
- 头部指针
- 尾部指针
方法:
- 创建/销毁
- 清空/判空
- 长度
- 遍历
- 插入(队尾)
- 删除 (队首)
私有:
- 指针
- 长度
- 容量
用途:排号机
剑指 Offer 59 - II. 队列的最大值
解决: 辅助数组
⚠️ 辅助数组与实际队列长度并非一样, 操作有区别。辅助队列 dequedeque 队首元素就是队列的最大值
MaxQueue.prototype.push_back = function(value) {this.queue.push(value);// 辅助队列队尾<valuewhile (this.maxQueue[this.maxQueue.length - 1] < value) {this.maxQueue.pop()}this.maxQueue.push(value)};/*** @return {number}*/MaxQueue.prototype.pop_front = function() {let cur = this.queue.shift()if (cur && cur == this.maxQueue[0]) {this.maxQueue.shift()}return cur || -1;};
测试数据
[1, 2]
[15, 9]
LC
1670. 设计前中后队列
题解: 双队列
