You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Initialize your data structure here. Set the size of the deque to be k. * @param {number} k */functionListNode(value){this.value=value;this.prev=this.next=null;}varMyCircularDeque=function(k){this.capacity=k;// 储存队列的容量this.length=0;// 储存队列的长度constnode=this.createNode();this.head=node;this.tail=node;};MyCircularDeque.prototype.createNode=function(value=-1){returnnewListNode(value);};/** * 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()){returnfalse;}// 创建新节点constnode=this.createNode(value);// 将新节点与队首节点相互连接this.head.next=node;node.prev=this.head;// 将头指针指向新队首节点this.head=node;// 队列长度+1this.length++;returntrue;};/** * 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()){returnfalse;}// 将新值存储在尾指针的节点上this.tail.value=value;// 创建新节点constnode=this.createNode();// 将新节点与尾指针的节点相互连接this.tail.prev=node;node.next=this.tail;// 尾指针指向新节点this.tail=node;// 队列长度+1this.length++;returntrue;};/** * Deletes an item from the front of Deque. Return true if the operation is successful. * @return {boolean} */MyCircularDeque.prototype.deleteFront=function(){// 判断队列是否为空if(this.isEmpty()){returnfalse;}// 缓存队首的上一个元素consttemp=this.head.prev;// 将队首与其上一个元素的连接互相打断this.head.prev=null;temp.next=null;// 将头指针移动到新队首this.head=temp;// 队列长度-1this.length--;returntrue;};/** * Deletes an item from the rear of Deque. Return true if the operation is successful. * @return {boolean} */MyCircularDeque.prototype.deleteLast=function(){// 判断队列是否为空if(this.isEmpty()){returnfalse;}// 缓存队尾元素consttemp=this.tail.next;// 将队尾元素的值设为-1,即删除了队尾的值temp.value=-1;// 将尾指针与队尾节点的连接打断this.tail.next=null;temp.prev=null;// 将尾指针指向新的插入对位元素的节点this.tail=temp;// 队列长度-1this.length--;returntrue;};/** * Get the front item from the deque. * @return {number} */MyCircularDeque.prototype.getFront=function(){// 队首的值可直接返回头节点的值returnthis.head.value;};/** * Get the last item from the deque. * @return {number} */MyCircularDeque.prototype.getRear=function(){// 如果队列为空,则返回-1if(this.isEmpty()){return-1;}// 如果队列不为空,则返回头对位的值returnthis.tail.next.value;};/** * Checks whether the circular deque is empty or not. * @return {boolean} */MyCircularDeque.prototype.isEmpty=function(){return!this.length;};/** * Checks whether the circular deque is full or not. * @return {boolean} */MyCircularDeque.prototype.isFull=function(){returnthis.length===this.capacity;};
The text was updated successfully, but these errors were encountered:
原题链接:https://leetcode-cn.com/problems/design-circular-deque/
解题思路:
如果你看到这题的时候,感到没有思路,可以先尝试其前导题目:622. 设计循环队列,以及我的题解LeetCode题解:622. 设计循环队列,使用双向链表,JavaScript,详细注释。
在默认用例通过后,可以补充测试如下两个用例,如果都能通过,那么这题基本就没问题了。
["MyCircularDeque","insertFront","getRear","insertFront","getRear","insertLast","getFront","getRear","getFront","insertLast","deleteLast","getFront"]\n[[3],[9],[],[9],[],[5],[],[],[],[8],[],[]]
["MyCircularDeque","insertFront","deleteLast","getRear","getFront","getFront","deleteFront","insertFront","insertLast","insertFront","getFront","insertFront"]\n[[4],[9],[],[],[],[],[],[6],[5],[9],[],[6]]
The text was updated successfully, but these errors were encountered: