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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章