LeetCode题解:641. 设计循环双端队列,使用队列,JavaScript,详细注释

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原题链接:"},{"type":"link","attrs":{"href":"https://leetcode-cn.com/problems/design-circular-deque/","title":""},"content":[{"type":"text","text":"https://leetcode-cn.com/problems/design-circular-deque/"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"解题思路:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你看到这题的时候,感到没有思路,可以先尝试其前导题目:"},{"type":"link","attrs":{"href":"https://leetcode-cn.com/problems/design-circular-queue/","title":""},"content":[{"type":"text","text":"622. 设计循环队列"}]},{"type":"text","text":",以及我的题解[LeetCode题解:622. 设计循环队列,使用数组,JavaScript,详细注释](https://leetcode-cn.com/problems/design-circular-queue/solution/leetcodeti-jie-622-she-ji-xun-huan-dui-lie-shi-yon/)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"创建数组:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"先按照队列的容量,创建相应长度的数组,所有元素都默认为-1。这样获得元素时,只要直接返回即可。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"使用head指针,指向队首元素。使用tail指针,指向将要从队尾插入元素的位置,及队尾+1。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"插入元素:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"从头部添加元素时,先将head指针向前移动一位,再将元素插入。从头部移除元素的操作相反。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"从尾部添加元素时,先将元素插入到tail指针的位置,再将tail指针向后移动一位。从尾部移除元素的操作相反。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"判断队列为空或已满:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"如果不断添加元素,最终head指针和tail指针会相遇,此时由于tail指针的位置已有head对应的值占据,则队列已满。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"如果不断删除元素,最终head指针和tail指针会相遇,此时它们指向的位置都为-1,则队列为空。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在默认用例通过后,可以补充测试如下两个用例,如果都能通过,那么这题基本就没问题了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"[\"MyCircularDeque\",\"insertFront\",\"getRear\",\"insertFront\",\"getRear\",\"insertLast\",\"getFront\",\"getRear\",\"getFront\",\"insertLast\",\"deleteLast\",\"getFront\"]\\n[[3],[9],[],[9],[],[5],[],[],[],[8],[],[]]"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"[\"MyCircularDeque\",\"insertFront\",\"deleteLast\",\"getRear\",\"getFront\",\"getFront\",\"deleteFront\",\"insertFront\",\"insertLast\",\"insertFront\",\"getFront\",\"insertFront\"]\\n[[4],[9],[],[],[],[],[],[6],[5],[9],[],[6]]"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"/**\n\n * Initialize your data structure here. Set the size of the deque to be k.\n\n * @param {number} k\n\n */\n\nvar MyCircularDeque = function (k) {\n\n // 队列的容量\n\n this.capacity = k;\n\n // 使用数组存放队列元素,所有的初始值都是-1,取值的时候直接返回\n\n this.queue = new Array(k).fill(-1);\n\n // 队列的头指针,即队列头元素的位置\n\n this.head = 0;\n\n // 队列的尾指针,即尾部要插入元素的位置,也就是队列的尾元素的位置+1\n\n this.tail = 0;\n\n};\n\n// 将index-1,需要考虑index到达数组首尾时需要循环\n\nMyCircularDeque.prototype.reduceIndex = function (index) {\n\n return (index + this.capacity - 1) % this.capacity;\n\n};\n\n// 将index+1,需要考虑index到达数组首尾时需要循环\n\nMyCircularDeque.prototype.addIndex = function (index) {\n\n return (index + 1) % this.capacity;\n\n};\n\n/**\n\n * Adds an item at the front of Deque. Return true if the operation is successful.\n\n * @param {number} value\n\n * @return {boolean}\n\n */\n\nMyCircularDeque.prototype.insertFront = function (value) {\n\n // 判断队列是否已满\n\n if (this.isFull()) {\n\n return false;\n\n }\n\n // 从头部插入元素时,要先将头指针向前移动一位\n\n this.head = this.reduceIndex(this.head);\n\n // 在新的头指针位置插入元素\n\n this.queue[this.head] = value;\n\n return true;\n\n};\n\n/**\n\n * Adds an item at the rear of Deque. Return true if the operation is successful.\n\n * @param {number} value\n\n * @return {boolean}\n\n */\n\nMyCircularDeque.prototype.insertLast = function (value) {\n\n // 判断队列是否已满\n\n if (this.isFull()) {\n\n return false;\n\n }\n\n // 在尾指针的位置插入元素\n\n this.queue[this.tail] = value;\n\n // 将尾指针向后移动一位,指向下一次插入元素的位置\n\n this.tail = this.addIndex(this.tail);\n\n return true;\n\n};\n\n/**\n\n * Deletes an item from the front of Deque. Return true if the operation is successful.\n\n * @return {boolean}\n\n */\n\nMyCircularDeque.prototype.deleteFront = function () {\n\n // 判断队列是否为空\n\n if (this.isEmpty()) {\n\n return false;\n\n }\n\n // 将头指针的值置为-1,表示元素被删除\n\n this.queue[this.head] = -1;\n\n // 删除元素后,要将头指针向后移动一位\n\n this.head = this.addIndex(this.head);\n\n return true;\n\n};\n\n/**\n\n * Deletes an item from the rear of Deque. Return true if the operation is successful.\n\n * @return {boolean}\n\n */\n\nMyCircularDeque.prototype.deleteLast = function () {\n\n // 判断队列是否为空\n\n if (this.isEmpty()) {\n\n return false;\n\n }\n\n // 先将尾指针向前移动一位,指向队尾元素\n\n this.tail = this.reduceIndex(this.tail);\n\n // 将队尾元素设置为-1\n\n this.queue[this.tail] = -1;\n\n return true;\n\n};\n\n/**\n\n * Get the front item from the deque.\n\n * @return {number}\n\n */\n\nMyCircularDeque.prototype.getFront = function () {\n\n // 直接返回头指针的元素即可,由于初始值是-1,因此如果队列为空,会返回-1\n\n return this.queue[this.head];\n\n};\n\n/**\n\n * Get the last item from the deque.\n\n * @return {number}\n\n */\n\nMyCircularDeque.prototype.getRear = function () {\n\n // 直接返回尾指针-1的元素即可,由于初始值是-1,因此如果队列为空,会返回-1\n\n return this.queue[this.reduceIndex(this.tail)];\n\n};\n\n/**\n\n * Checks whether the circular deque is empty or not.\n\n * @return {boolean}\n\n */\n\nMyCircularDeque.prototype.isEmpty = function () {\n\n // 如果头尾指针的位置相同,且对应位置的值为-1,表示队列中已无元素,则为空\n\n return this.head === this.tail && this.queue[this.head] < 0;\n\n};\n\n/**\n\n * Checks whether the circular deque is full or not.\n\n * @return {boolean}\n\n */\n\nMyCircularDeque.prototype.isFull = function () {\n\n // 如果头尾指针的位置相同,且对应位置的值不为-1,此时无法再插入元素,则队列已满\n\n return this.head === this.tail && this.queue[this.head] >= 0;\n\n};"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章