当前位置: 首页 > news >正文

leetcode641. 设计循环双端队列

设计实现双端队列。

实现 MyCircularDeque 类:

  • MyCircularDeque(int k) :构造函数,双端队列最大为 k 。
  • boolean insertFront():将一个元素添加到双端队列头部。 如果操作成功返回 true ,否则返回 false 。
  • boolean insertLast() :将一个元素添加到双端队列尾部。如果操作成功返回 true ,否则返回 false 。
  • boolean deleteFront() :从双端队列头部删除一个元素。 如果操作成功返回 true ,否则返回 false 。
  • boolean deleteLast() :从双端队列尾部删除一个元素。如果操作成功返回 true ,否则返回 false 。
  • int getFront() ):从双端队列头部获得一个元素。如果双端队列为空,返回 -1 。
  • int getRear() :获得双端队列的最后一个元素。 如果双端队列为空,返回 -1 。
  • boolean isEmpty() :若双端队列为空,则返回 true ,否则返回 false  。
  • boolean isFull() :若双端队列满了,则返回 true ,否则返回 false 。

示例 1:

输入
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
输出
[null, true, true, true, false, 2, true, true, true, 4]
/*** Initialize your data structure here. Set the size of the deque to be k.* @param {number} k*/
var MyCircularDeque = function(k) {this.queue = new Array(k+1);//用来模拟队列的数组this.front = 0;//头指针this.rear = 0;//尾指针this.max = 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.isFull()) return false;this.front = (this.front + this.max)  % (this.max + 1);this.queue[this.front] = 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.isFull()) return false;this.queue[this.rear] = value;this.rear = (this.rear + 1) % (this.max + 1);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.isEmpty()) return false;//  删除的队列头部,this.front往后移动一位this.front = (this.front + 1) % (this.max + 1);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.isEmpty()) return false;//   删除队列的最后一个元素,那么尾指针rear,往前移动一位this.rear = (this.rear+this.max) % (this.max + 1);return true;
};/*** Get the front item from the deque.* @return {number}*/
MyCircularDeque.prototype.getFront = function() {if(this.isEmpty()) return -1;return this.queue[this.front]
};/*** Get the last item from the deque.* @return {number}*/
MyCircularDeque.prototype.getRear = function() {if(this.isEmpty()) return -1;return this.queue[(this.rear+this.max) % (this.max + 1)];
};/*** Checks whether the circular deque is empty or not.* @return {boolean}*/
MyCircularDeque.prototype.isEmpty = function() {return ((this.rear - this.front + this.max+1) % (this.max+1)) == 0;
};/*** Checks whether the circular deque is full or not.* @return {boolean}*/
MyCircularDeque.prototype.isFull = function() {return ((this.rear - this.front + this.max+1) % (this.max+1)) == this.max;
};/*** Your MyCircularDeque object will be instantiated and called as such:* var obj = new MyCircularDeque(k)* var param_1 = obj.insertFront(value)* var param_2 = obj.insertLast(value)* var param_3 = obj.deleteFront()* var param_4 = obj.deleteLast()* var param_5 = obj.getFront()* var param_6 = obj.getRear()* var param_7 = obj.isEmpty()* var param_8 = obj.isFull()*/


http://www.mrgr.cn/news/38812.html

相关文章:

  • Vue 之组件插槽Slot用法(组件间通信一种方式)
  • 磁盘管理器
  • python 02 List
  • C#知识|基于反射和接口实现抽象工厂设计模式
  • 四个方法,隐藏word文件中的内容!
  • 零基础学习AI大模型,手把手带你从入门到实践!看过的已经月入6w了!
  • 【Python大语言模型系列】开源机器人对话系统框架RASA介绍与使用(案例分析)
  • 电子相册|智能化电子相册|基于java的电子相册管理系统设计与实现(源码+数据库+文档)
  • 【吊打面试官系列-MySQL面试题】优化MySQL数据库的方法?
  • win10安装Java闪退
  • 我把「国产Sora」接入了「小爱」,邀你免费体验
  • GAMES101(21~22节,动画和仿真)
  • 远程服务器安装anaconda并创建虚拟环境
  • 基于python+spark的外卖餐饮数据分析系统设计与实现(含论文)-Spark毕业设计选题推荐
  • LLM基础概念:RAG
  • 【系统方案】智慧城市大数据平台建设方案(Word)
  • GCC使用入门
  • HAL库I2C通用驱动程序(HAL I2C Generic Driver)
  • H.264编解码工具 - NVIDIA CUDA
  • 余生,闲话少说,闲事少管,闲人少理(深度)