From 42e2d3f09b6e34d541991cb0b1f1b3a9510aa5d2 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 14 Apr 2020 19:09:17 +0800 Subject: [PATCH 001/145] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E5=85=83?= =?UTF-8?q?=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...73\351\231\244\345\205\203\347\264\240.js" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" diff --git "a/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" "b/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" new file mode 100644 index 0000000..196c01e --- /dev/null +++ "b/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" @@ -0,0 +1,52 @@ +/** + 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。 + + 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 + + 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 + +   + + 示例 1: + + 给定 nums = [3,2,2,3], val = 3, + + 函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。 + + 你不需要考虑数组中超出新长度后面的元素。 + 示例 2: + + 给定 nums = [0,1,2,2,3,0,4,2], val = 2, + + 函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。 + + 注意这五个元素可为任意顺序。 + + 你不需要考虑数组中超出新长度后面的元素。 + + 来源:力扣(LeetCode) + 链接:https://leetcode-cn.com/problems/remove-element + 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ + +var removeElement = function (nums, val) { + let i = 0; + let j = 0; + + while (i < nums.length) { + let num = nums[i]; + if (num !== val) { + nums[j] = num; + j++; + } + + i++; + } + return j; +}; + +removeElement([3, 2, 2, 3], 3); + +/** + * 这题用的是双指针法,慢指针指向待替换的位置,等到读取到非目标值的时候,就替换到那个位置,然后把慢指针前进。 + */ From 0d7660438b99fa4ef44ac06844a1a15633ccc12b Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 14 Apr 2020 22:21:43 +0800 Subject: [PATCH 002/145] =?UTF-8?q?feat:=20=E6=9C=89=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\346\261\202\344\272\244\351\233\206.js" | 79 +++++++++++++++++++ ...44\270\252\345\205\203\347\264\240-540.js" | 61 ++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 "\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" create mode 100644 "\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" diff --git "a/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" "b/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" new file mode 100644 index 0000000..35bff36 --- /dev/null +++ "b/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" @@ -0,0 +1,79 @@ +var a = [] + +for (let index = 0; index < 500; index++) { + a.push(i) +} + +for (var i = 16; i < 10000; i++) { + a.push(i) +} + +var b = [] + +for (let index = 0; index < 500; index++) { + b.push(i) +} + +for (var i = 10001; i < 50000; i++) { + b.push(i) +} + +function mapIntersection(arr1, arr2) { + console.time() + var map = new Map() + for (var i = 0; i < arr1.length; i++) { + map.set(arr1[i], true) + } + + var result = [] + for (var i = 0; i < arr2.length; i++) { + var val = arr2[i] + if (map.get(val)) { + result.push(val) + } + } + console.timeEnd() + return result +} + +console.log("mapIntersection", mapIntersection(a, b)) + +function pointIntersection(arr1, arr2) { + console.time() + let i = 0 + let j = 0 + + let l1 = arr1.length + let l2 = arr2.length + + let result = [] + while (i < l1 && j < l2) { + let val1 = arr1[i] + let val2 = arr2[j] + let val1Last = arr1[i - 1] + let val2Last = arr2[j - 1] + + if (val1 > val2) { + i++ + } + + if (val1 === val2) { + result.push(val1) + i++ + j++ + } + + if (val1 < val2) { + j++ + } + + if (val1 > val2Last || val2 > val1Last) { + console.timeEnd() + return result + } + } + console.timeEnd() + return result +} + +console.log('pointIntersection', pointIntersection(a, b)) \ No newline at end of file diff --git "a/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" "b/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" new file mode 100644 index 0000000..bb30ae6 --- /dev/null +++ "b/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" @@ -0,0 +1,61 @@ +/** + 540. 有序数组中的单一元素 + 给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。 + + 示例 1: + + 输入: [1,1,2,3,3,4,4,8,8] + 输出: 2 + 示例 2: + + 输入: [3,3,7,7,10,11,11] + 输出: 10 + 注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行。 + */ + +function singleNonDuplicate(nums) { + let len = nums.length + + let lo = 0 + let hi = len - 1 + + while (lo < hi) { + let mid = lo + Math.floor((hi - lo) / 2) + let midVal = nums[mid] + + // 被mid后的数组长度是奇数还是偶数 + let halvesAreEven = (hi - mid) % 2 == 0; + if (midVal === nums[mid - 1]) { + if (halvesAreEven) { + // [0, 1, 1, 2, 2] + // mid + // 显然左边是奇数 [0] hi是mid往左两位 + hi = mid - 2 + }else { + // [0, 0, 1, 1, 2, 2, 3] + // mid + // 显然右边是奇数 [2, 2, 3] lo是mid往右一位 + lo = mid + 1 + } + + }else if (midVal === nums[mid + 1]) { + if (halvesAreEven) { + // [0, 0, 1, 1, 2] + // mid + // 右边是奇数 [2] lo是mid右移动两位 + lo = mid + 2 + }else { + // [0, 0, 1, 2, 2, 3, 3] + // mid + // 左边是奇数 [0, 0, 1] hi是mid左移一位 + hi = mid - 1 + } + } else { + return midVal + } + } + return nums[lo] +} + + +console.log(singleNonDuplicate([3,3,7,7,10,11,11])) \ No newline at end of file From e9119f1b8708e883cf2f3db75a5987424fb19791 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 15 Apr 2020 19:50:12 +0800 Subject: [PATCH 003/145] =?UTF-8?q?feat:=20=E5=B2=9B=E5=B1=BF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\345\261\277\346\225\260\351\207\217.js" | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 "\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" diff --git "a/\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" "b/\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" new file mode 100644 index 0000000..02fdba3 --- /dev/null +++ "b/\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" @@ -0,0 +1,62 @@ +/** + * 岛屿问题 + + 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。 + + 示例 1: + + 输入: + 11110 + 11010 + 11000 + 00000 + + 输出: 1 + 示例 2: + + 输入: + 11000 + 11000 + 00100 + 00011 + + 输出: 3 + + 来源:力扣(LeetCode) + 链接:https://leetcode-cn.com/problems/number-of-islands + 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ +var numIslands = function (grid) { + let count = 0 + for (let i = 0; i < grid.length; i++) { + let row = grid[i] + for (let j = 0; j < row.length; j++) { + if (row[j] === '1') { + dfs(grid, i, j) + count++ + } + } + } + return count +}; + +function dfs(grid, i, j) { + let point = grid[i] && grid[i][j] + if (point === '0' || point === undefined) { + return + } + grid[i][j] = '0' + + dfs(grid, i - 1, j) // 上 + dfs(grid, i + 1, j) // 下 + dfs(grid, i, j - 1) // 左 + dfs(grid, i, j + 1) // 右 +} + +/** + * 很经典的 dfs 问题,很棒很棒的思路,当找到一个点为1的时候,先记录数量加一。 + + 然后对于每个点递归的去遍历自己和上下左右的节点,如果值为1就置为0再继续遍历上下左右,这样直到遇到某一边本身就为0的时候停止。 + + 这样,一片 “值为1的岛屿” 就全部归零了。 + */ \ No newline at end of file From 5d5f2c24c2e8b30391c86e41df0c55729ef4f091 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 16 Apr 2020 11:36:33 +0800 Subject: [PATCH 004/145] =?UTF-8?q?feat:=20bfs=E5=92=8Cdfs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\345\205\210\351\201\215\345\216\206.js" | 59 +++++++++++++++++++ ...73\351\231\244\345\205\203\347\264\240.js" | 1 + 2 files changed, 60 insertions(+) create mode 100644 "\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" diff --git "a/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" "b/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" new file mode 100644 index 0000000..ae6162c --- /dev/null +++ "b/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" @@ -0,0 +1,59 @@ +const data = [ + { + name: "1", + children: [ + { + name: "1-1", + }, + { + name: "1-2", + children: [ + { + name: "1-2-1", + }, + { + name: "1-2-2", + }, + ], + }, + { + name: "1-3", + children: [ + { + name: "1-3-1", + }, + { + name: "1-3-2", + }, + ], + }, + ], + }, +] + +function bfs(tree) { + let queue = [] + tree.forEach((node) => { + console.log(node.name) + if (node.children) { + queue = queue.concat(node.children) + } + }) + if (queue.length) { + bfs(queue) + } +} + +function dfs(tree) { + tree.forEach((node) => { + console.log(node.name) + if (node.children) { + dfs(node.children) + } + }) +} + +console.log('bfs') +bfs(data) +console.log('dfs') +dfs(data) \ No newline at end of file diff --git "a/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" "b/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" index 196c01e..c03f041 100644 --- "a/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" +++ "b/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" @@ -42,6 +42,7 @@ var removeElement = function (nums, val) { i++; } + return j; }; From 2feda62a3ac8b61dbb4b1fdc2cfd408eb2975220 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 16 Apr 2020 11:39:49 +0800 Subject: [PATCH 005/145] =?UTF-8?q?feat:=20bfs=E5=92=8Cdfs=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\274\230\345\205\210\351\201\215\345\216\206.js" | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git "a/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" "b/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" index ae6162c..4a17984 100644 --- "a/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" +++ "b/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" @@ -31,6 +31,10 @@ const data = [ }, ] +/** + * bfs需要借助一个队列,在访问平级节点的时候遇到节点有子节点的话,先全部把子节点推入队列。 + * 这样下一轮bfs的时候,就会按顺序先平级执行这个队列,在这个过程中重复上一步对子节点的收集。 + */ function bfs(tree) { let queue = [] tree.forEach((node) => { @@ -44,6 +48,9 @@ function bfs(tree) { } } +/** + * dfs就是一路向下访问,遇到节点有children的情况就直接递归下去,而先不管同级的其他节点。 + */ function dfs(tree) { tree.forEach((node) => { console.log(node.name) @@ -53,7 +60,7 @@ function dfs(tree) { }) } -console.log('bfs') +console.log("bfs") bfs(data) -console.log('dfs') -dfs(data) \ No newline at end of file +console.log("dfs") +dfs(data) From e4a188c333ae520ac22f776fb41d9c8b57c3760d Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 26 Apr 2020 00:09:39 +0800 Subject: [PATCH 006/145] =?UTF-8?q?feat:=20=E6=A0=87=E9=A2=98=E5=88=86?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...07\351\242\230\345\210\206\347\273\204.js" | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 "\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" diff --git "a/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" "b/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" new file mode 100644 index 0000000..4847155 --- /dev/null +++ "b/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" @@ -0,0 +1,105 @@ +/** + * 需要输出 + [{ + "name": "h3" + }, { + "name": "h2", + "child": [{ + "name": "h3" + }] + }, { + "name": "h1", + "child": [{ + "name": "h2", + "child": [{ + "name": "h3" + }, { + "name": "h3" + }] + }, { + "name": "h2", + "child": [{ + "name": "h3" + }] + }] + }, { + "name": "h1", + "child": [{ + "name": "h2", + "child": [{ + "name": "h4" + }] + }, { + "name": "h2", + "child": [{ + "name": "h3" + }] + }] + }] + */ + +let list = [ + 'h3', + 'h2', 'h3', + 'h1', 'h2', 'h3', 'h3', 'h2', 'h3', + 'h1', 'h2', 'h4', 'h2', 'h3' +] + +function makeTree(arr) { + let tree = []; + let max = Infinity + let prev = {}; + + arr.forEach((title) => { + let level = Number(title[1]); + if (level <= max) { + let node = { + name: title, + }; + tree.push(node); + prev.level = level; + prev.node = node; + max = level + } else if (level === prev.level) { + // 等级相同的话 上级节点的child继续增加子节点 + prev = prev.prev; + pushNodeAndAdvanceQueue(); + } else if (level > prev.level) { + pushNodeAndAdvanceQueue(); + } else { + while (level <= prev.level) { + // 向上回溯,找到平级的再上一层node + prev = prev.prev; + } + pushNodeAndAdvanceQueue(); + } + + function pushNodeAndAdvanceQueue() { + let node = { + name: title, + }; + if (!prev.node.child) { + let child = []; + prev.node.child = child; + } + prev.node.child.push(node); + let next = { + level, + node, + prev, + }; + prev = next; + } + }); + + return tree; +} + +console.log(makeTree(list)); + +/** + * 思路 + * + * 利用链表来向上回溯合适的父节点 + * 利用max变量记录当前分组的最大标题,如果比max还大,就需要新开一个分组了。 + */ From 87e92b2e1d6413255b0fb56d1f2b69864e588694 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 26 Apr 2020 12:45:33 +0800 Subject: [PATCH 007/145] =?UTF-8?q?feat:=20=E6=A0=87=E7=AD=BE=E5=88=86?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\240\207\351\242\230\345\210\206\347\273\204.js" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" "b/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" index 4847155..8bd5c54 100644 --- "a/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" +++ "b/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" @@ -1,5 +1,5 @@ /** - * 需要输出 + 需要输出 [{ "name": "h3" }, { From c94285382f6eca92d76aef5b1be95f02c57da907 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Mon, 27 Apr 2020 21:16:36 +0800 Subject: [PATCH 008/145] =?UTF-8?q?feat:=20=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc | 3 + ...7-\345\276\252\347\216\257\347\211\210.js" | 142 ++++++++++ ...25\347\232\204\345\214\272\345\237\237.js" | 257 ++++++++++++++++++ 3 files changed, 402 insertions(+) create mode 100644 .prettierrc create mode 100644 "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" create mode 100644 "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..6711c26 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "printWidth": 140 +} \ No newline at end of file diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" new file mode 100644 index 0000000..4a1e385 --- /dev/null +++ "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" @@ -0,0 +1,142 @@ +/** +给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。 + +找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。 + +示例: + +X X X X +X O O X +X X O X +X O X X +运行你的函数后,矩阵变为: + +X X X X +X X X X +X X X X +X O X X +解释: + +被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。 + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/surrounded-regions +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ + +/** + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ + +function genKey(i, j) { + return `${i}-${j}`; +} + +var solve = function (board) { + const notXMap = {}; + for (let i = 0; i < board.length; i++) { + let row = board[i]; + for (let j = 0; j < row.length; j++) { + let cell = row[j]; + if (cell === "O" && !notXMap[genKey(i, j)]) { + dfsIsValid(board, i, j, notXMap); + } + } + } + return board; +}; + +function getBoradValue(board, i, j) { + let row = board[i]; + return row && row[j]; +} + +function dfsIsValid(board, currentI, currnetJ, notXMap) { + let queue = []; // 本次dfs走过的路径 + let thisTimeFailed = { current: false }; + let thisTimeHasHandledMap = {}; + let checkQueue = [[currentI, currnetJ]]; + + while (checkQueue.length) { + const head = checkQueue.shift(); + // 临时替换 标记为可替换节点 + const [i, j] = head; + let row = board[i]; + let cell = row && board[i][j]; + + // 已经在map中记录处理过的 整个链路都不可能为X + if (notXMap[genKey(i, j)]) { + continue; + } + + // 本次路径失败 + if (!cell) { + thisTimeFailed.current = true; + continue; + } + + // 为X说明这一边符合条件 + if (cell === "X") continue; + + // 推到路径队列里,等到遍历完成决定该保留O还是换成X + queue.push([i, j]); + + if (!thisTimeHasHandledMap[genKey(i - 1, j)] && !notXMap[genKey(i - 1, j)]) { + thisTimeHasHandledMap[genKey(i - 1, j)] = true; + checkQueue.push([i - 1, j]) + }; + if (!thisTimeHasHandledMap[genKey(i + 1, j)] && !notXMap[genKey(i + 1, j)]) { + thisTimeHasHandledMap[genKey(i + 1, j)] = true; + checkQueue.push([i + 1, j]) + }; + if (!thisTimeHasHandledMap[genKey(i, j - 1)] && !notXMap[genKey(i, j - 1)]) { + thisTimeHasHandledMap[genKey(i, j - 1)] = true; + checkQueue.push([i, j - 1]) + }; + if (!thisTimeHasHandledMap[genKey(i, j + 1)] && !notXMap[genKey(i, j + 1)]) { + thisTimeHasHandledMap[genKey(i, j + 1)] = true; + checkQueue.push([i, j + 1]); + } + } + + // 循环结束后 + // 如果本次循环遇到出界的情况 则全部记录在map中 后续遍历不再走这些格子 + // 否则说明是被围绕的区域 赋值为X + if (queue.length) { + queue.forEach(([i, j]) => { + // 失败了 恢复O + if (thisTimeFailed.current) { + // 记录在map中 一定不会是X + notXMap[genKey(i, j)] = true; + } else { + board[i][j] = "X"; + } + }); + } +} + +console.log( + solve([ + ["X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"], + ["O", "X", "O", "O", "O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "X"], + ["O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O"], + ["O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "O", "O", "O", "O", "X", "O", "X", "O", "X", "O", "X", "O", "X", "O"], + ["O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O"], + ["X", "O", "O", "O", "X", "X", "X", "O", "X", "O", "O", "O", "O", "X", "X", "O", "X", "O", "O", "O"], + ["O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O"], + ["X", "O", "O", "O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "O", "O", "X", "O", "O", "X"], + ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "X", "O", "X"], + ["O", "O", "O", "O", "X", "O", "X", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O"], + ["X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"], + ["O", "X", "O", "X", "O", "O", "O", "X", "O", "X", "O", "O", "O", "X", "O", "X", "O", "X", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "O", "X", "O", "X", "O"], + ["X", "X", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X", "X", "O", "O", "O", "X", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "X", "O", "X", "O", "O"], + ["O", "O", "O", "X", "O", "O", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "O", "O", "X", "O"], + ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"], + ["X", "O", "O", "O", "O", "X", "O", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "O"], + ]) +); diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" new file mode 100644 index 0000000..55bc3f1 --- /dev/null +++ "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" @@ -0,0 +1,257 @@ +/** +给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。 + +找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。 + +示例: + +X X X X +X O O X +X X O X +X O X X +运行你的函数后,矩阵变为: + +X X X X +X X X X +X X X X +X O X X +解释: + +被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。 + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/surrounded-regions +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ + +/** + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ + +function genKey(i, j) { + return `${i}-${j}`; +} + +var solve = function (board) { + const notXMap = {}; + for (let i = 0; i < board.length; i++) { + let row = board[i]; + for (let j = 0; j < row.length; j++) { + let cell = row[j]; + if (cell === "O") { + let queue = []; // 本次dfs走过的路径 + let thisDfsFailed = { current: false }; + dfsIsValid(board, i, j, notXMap, queue, thisDfsFailed); + } + } + } + for (let i = 0; i < board.length; i++) { + let row = board[i]; + for (let j = 0; j < row.length; j++) { + let cell = row[j]; + if (cell === "O" || cell === "SYMBOL") { + // 如果是X + if (!notXMap[genKey(i, j)]) { + board[i][j] = "X"; + } + } + } + } + return board; +}; + +function dfsIsValid(board, i, j, notXMap, queue, thisDfsFailed) { + // 临时替换 标记为可替换节点 + let row = board[i]; + let cell = row && board[i][j]; + + // 已经在map中记录处理过的 整个链路都不可能为X + if (notXMap[genKey(i, j)]) { + return; + } + + // 本次路径失败 + if (!cell) { + thisDfsFailed.current = true; + return; + } + + // 为X说明这一边符合条件 + if (cell === "X") return; + // 为SYMBOL说明之前已经被临时标记过 继续检查别的边 + if (cell === "SYMBOL") return; + + board[i][j] = "SYMBOL"; + // 这里才去记录路径,排除上面不需要处理的格子。 + queue.push({ i, j }); + + // 为O的话,进一步DFS,直到遇到「边界(X、SYMBOL、undefined)」 + dfsIsValid(board, i - 1, j, notXMap, queue, thisDfsFailed); // 上 + dfsIsValid(board, i + 1, j, notXMap, queue, thisDfsFailed); // 下 + dfsIsValid(board, i, j - 1, notXMap, queue, thisDfsFailed); // 左 + dfsIsValid(board, i, j + 1, notXMap, queue, thisDfsFailed); // 右 + + // 如果DFS结束,queue的值没有被清空,说明这是符合条件的 + if (queue.length) { + queue.forEach(({ i, j }) => { + // 失败了 恢复O + if (thisDfsFailed.current) { + board[i][j] = "O"; + // 记录在map中 一定不会是X + notXMap[genKey(i, j)] = true; + } + }); + } +} + +console.log( + solve([ + ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], + ["X", "O", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], + ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], + ["O", "X", "O", "X", "X", "O", "X", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "O", "X"], + ["X", "O", "O", "X", "O", "X", "O", "O", "O", "X", "X", "O", "X", "O", "O", "X", "O", "O", "O", "O"], + ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "X", "O", "O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], + ["O", "O", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], + ["X", "O", "X", "O", "X", "O", "O", "X", "X", "X", "O", "X", "X", "O", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "X", "O"], + ["X", "O", "O", "O", "X", "X", "X", "O", "X", "O", "O", "O", "X", "O", "X", "O", "X", "O", "O", "X"], + ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O"], + ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "O", "O", "X", "X", "O", "X", "O"], + ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "O", "X", "O", "O", "X", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "O", "X", "O", "O", "X", "O", "O", "X", "X", "X"], + ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "O", "X", "X", "X", "O", "O", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "O", "X", "O", "X", "O", "O", "O", "X", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], + ]) +); + +//输入 +[ + ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], + ["X", "O", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], + ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], + ["O", "X", "O", "X", "X", "O", "X", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "O", "X"], + ["X", "O", "O", "X", "O", "X", "O", "O", "O", "X", "X", "O", "X", "O", "O", "X", "O", "O", "O", "O"], + ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "X", "O", "O", "O", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], + ["O", "O", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], + ["X", "O", "X", "O", "X", "O", "O", "X", "X", "X", "O", "X", "X", "O", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "X", "O"], + ["X", "O", "O", "O", "X", "X", "X", "O", "X", "O", "O", "O", "X", "O", "X", "O", "X", "O", "O", "X"], + ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O"], + ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "O", "O", "X", "X", "O", "X", "O"], + ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "O", "X", "O", "O", "X", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "O", "X", "O", "O", "X", "O", "O", "X", "X", "X"], + ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "O", "X", "X", "X", "O", "O", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "O", "X", "O", "X", "O", "O", "O", "X", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], +]; + +// 输出 +[ + ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], + ["X", "X", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], + ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], + ["O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X"], + ["X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "X", "O", "O", "O", "O"], + ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], + ["O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], + ["X", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "X", "O"], + ["X", "O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O", "O", "X"], + ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O"], + ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O"], + ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "X"], + ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], +]; + +// 期望 +[ + ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], + ["X", "X", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], + ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], + ["O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X"], + ["X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "X", "O", "O", "O", "O"], + ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], + ["O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], + ["X", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "O"], + ["X", "O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O", "O", "X"], + ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O"], + ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O"], + ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "X"], + ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], +]; + +[ + ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], + ["X", "X", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], + ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], + ["O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X"], + ["X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "X", "O", "O", "O", "O"], + ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], + ["O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], + ["X", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "O", "O", "O"], + ["O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "O"], + ["X", "O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O", "O", "X"], + ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O"], + ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O"], + ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X"], + ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "X"], + ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O"], + ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "X"], + ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], +]; +// ;[ +// ["X", "X", "X", "X", "O", "O", "X", "X", "O"], +// ["O", "O", "O", "O", "X", "X", "O", "O", "X"], +// ["X", "O", "X", "O", "O", "X", "X", "O", "X"], +// ["O", "O", "X", "X", "X", "O", "O", "O", "O"], +// ["X", "O", "O", "X", "X", "X", "X", "X", "O"], +// ["O", "O", "X", "O", "X", "O", "X", "O", "X"], +// ["O", "O", "O", "X", "X", "O", "X", "O", "X"], +// ["O", "O", "O", "X", "O", "O", "O", "X", "O"], +// ["O", "X", "O", "O", "O", "X", "O", "X", "O"], +// ];[ +// (["X", "X", "X", "X", "O", "O", "X", "X", "O"], +// ["O", "O", "O", "O", "X", "X", "X", "X", "X"], +// ["X", "O", "X", "O", "O", "X", "X", "X", "X"], +// ["O", "O", "X", "X", "X", "X", "X", "X", "O"], +// ["X", "O", "O", "X", "X", "X", "X", "X", "O"], +// ["O", "O", "X", "X", "X", "O", "X", "X", "X"], +// ["O", "O", "O", "X", "X", "O", "X", "X", "X"], +// ["O", "O", "O", "X", "O", "O", "O", "X", "O"], +// ["O", "X", "O", "O", "O", "X", "O", "X", "O"]) +// ];[ +// (["X", "X", "X", "X", "O", "O", "X", "X", "O"], +// ["O", "O", "O", "O", "X", "X", "O", "O", "X"], +// ["X", "O", "X", "O", "O", "X", "X", "O", "X"], +// ["O", "O", "X", "X", "X", "O", "O", "O", "O"], +// ["X", "O", "O", "X", "X", "X", "X", "X", "O"], +// ["O", "O", "X", "X", "X", "O", "X", "X", "X"], +// ["O", "O", "O", "X", "X", "O", "X", "X", "X"], +// ["O", "O", "O", "X", "O", "O", "O", "X", "O"], +// ["O", "X", "O", "O", "O", "X", "O", "X", "O"]) +// ] From 4df3be6ef2ffb3206df857a93c21ab3b128a7544 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 28 Apr 2020 01:53:06 +0800 Subject: [PATCH 009/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=88=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE-bobo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc | 3 ++- ...14\345\210\206\346\237\245\346\211\276.js" | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 "\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" diff --git a/.prettierrc b/.prettierrc index 6711c26..7bff574 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,3 +1,4 @@ { - "printWidth": 140 + "printWidth": 140, + "tabWidth": 2 } \ No newline at end of file diff --git "a/\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" "b/\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" new file mode 100644 index 0000000..04e5f42 --- /dev/null +++ "b/\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" @@ -0,0 +1,21 @@ +function binarySearch(arr, n, target) { + // 在[l...r]的范围里寻找target + let l = 0; + let r = n - 1; + while (l <= r) { + let mid = Math.round((l + r) / 2); + + if (arr[mid] === target) { + return mid; + } + + if (target > arr[mid]) { + l = mid + 1; // 在[mid+1...r]的范围里寻找target + } else { + r = mid - 1; + } + } + return -1; +} + +console.log(console.log(binarySearch([1, 2, 3, 4, 5], 5, 2))); From 901fc5515d43995ff2e82135ccefe7dc3afe144b Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 28 Apr 2020 15:10:00 +0800 Subject: [PATCH 010/145] =?UTF-8?q?feat:=20=E8=A2=AB=E5=9B=B4=E7=BB=95?= =?UTF-8?q?=E7=9A=84=E5=B2=9B=E5=B1=BF=20=E4=BC=98=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.md" | 47 +++++++++++++++++++ ...7-\345\276\252\347\216\257\347\211\210.js" | 0 ...7-\347\256\200\346\264\201\347\211\210.js" | 44 +++++++++++++++++ ...25\347\232\204\345\214\272\345\237\237.js" | 0 4 files changed, 91 insertions(+) create mode 100644 "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" rename "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" => "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" (100%) create mode 100644 "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" rename "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" => "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" (100%) diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" new file mode 100644 index 0000000..acd4db2 --- /dev/null +++ "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" @@ -0,0 +1,47 @@ +# 社区看到的优解 + +其实这题本身是我想复杂了,我是一个个格子去遍历,然后再上下左右去扩展延伸。 + +但是其实只需要遍历四个边界上的节点,遇到 O 的边界点才开始蔓延遍历,并且把遍历到的节点都标记为 M(防止重复遍历) + +最后再一次性遍历整个二维数组,遇到 M 的标记都转为 O(因为是从边界蔓延的,一定是不符合 X 的条件的)。 + +这样遍历所走的路就会少很多。 + +```js +var solve = function (board) { + if (board.length == 0) return null; + + for (var y = 0; y < board.length; y++) { + for (var x = 0; x < board[0].length; x++) { + if (board[y][x] == "O" && (y == 0 || y == board.length - 1 || x == 0 || x == board[0].length - 1)) { + dfs(board, y, x); + } + } + } + + for (var y = 0; y < board.length; y++) { + for (var x = 0; x < board[0].length; x++) { + if (board[y][x] == "W") { + board[y][x] = "O"; + } else { + board[y][x] = "X"; + } + } + } + + return board; +}; + +function dfs(board, y, x) { + if (y < 0 || x < 0 || y >= board.length || x >= board[0].length || board[y][x] == "X" || board[y][x] == "W") { + return; + } + board[y][x] = "W"; + dfs(board, y + 1, x); + dfs(board, y - 1, x); + dfs(board, y, x + 1); + dfs(board, y, x - 1); + return; +} +``` diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" rename to "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" new file mode 100644 index 0000000..dce3133 --- /dev/null +++ "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" @@ -0,0 +1,44 @@ +var solve = function (board) { + if (board.length == 0) return null; + + for (var y = 0; y < board.length; y++) { + for (var x = 0; x < board[0].length; x++) { + if (board[y][x] == "O" && (y == 0 || y == board.length - 1 || x == 0 || x == board[0].length - 1)) { + dfs(board, y, x); + } + } + } + + for (var y = 0; y < board.length; y++) { + for (var x = 0; x < board[0].length; x++) { + if (board[y][x] == "W") { + board[y][x] = "O"; + } else { + board[y][x] = "X"; + } + } + } + + return board; +}; + +function dfs(board, y, x) { + if (y < 0 || x < 0 || y >= board.length || x >= board[0].length || board[y][x] == "X" || board[y][x] == "W") { + return; + } + board[y][x] = "W"; + dfs(board, y + 1, x); + dfs(board, y - 1, x); + dfs(board, y, x + 1); + dfs(board, y, x - 1); + return; +} + +console.log( + solve([ + ["X", "X", "X", "X"], + ["X", "O", "O", "X"], + ["X", "X", "O", "X"], + ["X", "O", "X", "X"], + ]) +); diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" "b/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" similarity index 100% rename from "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" rename to "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" From 6a327b25deb5f9b2fa4dcc213e4885006b4a9efc Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 29 Apr 2020 00:33:39 +0800 Subject: [PATCH 011/145] =?UTF-8?q?feat:=20=E5=B2=9B=E5=B1=BF=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E9=9D=A2=E7=A7=AF=20=E8=B0=83=E6=95=B4=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DFS\351\227\256\351\242\230/index.md" | 0 ...45\244\247\351\235\242\347\247\257-659.js" | 41 +++++++++++++++++++ ...7-\345\276\252\347\216\257\347\211\210.js" | 0 ...7-\347\256\200\346\264\201\347\211\210.js" | 0 ...25\347\232\204\345\214\272\345\237\237.js" | 0 5 files changed, 41 insertions(+) rename "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" => "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/index.md" (100%) create mode 100644 "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" rename "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" => "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" (100%) rename "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" => "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" (100%) rename "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" => "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" (100%) diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/index.md" similarity index 100% rename from "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/index.md" rename to "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/index.md" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" new file mode 100644 index 0000000..55ce898 --- /dev/null +++ "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" @@ -0,0 +1,41 @@ +// https://github.com/sl1673495/daily-plan/issues/18 + +/** + * @param {number[][]} grid + * @return {number} + */ +var maxAreaOfIsland = function (grid) { + let yLen = grid.length; + if (!yLen) return grid; + let xLen = grid[0].length; + let max = 0; + + for (let y = 0; y < yLen; y++) { + for (let x = 0; x < xLen; x++) { + if (grid[y][x] === 1) { + let countRef = { current: 0 }; + dfs(grid, y, x, countRef); + if (countRef.current > max) { + max = countRef.current; + } + } + } + } + return max; +}; + +function dfs(grid, y, x, countRef) { + if (!grid[y] || !grid[y][x] || grid[y][x] === 0 || grid[y][x] === "COMPLETE") { + return; + } + + if (grid[y][x] === 1) { + grid[y][x] = "COMPLETE"; + countRef.current++; + } + + dfs(grid, y - 1, x, countRef); + dfs(grid, y + 1, x, countRef); + dfs(grid, y, x - 1, countRef); + dfs(grid, y, x + 1, countRef); +} \ No newline at end of file diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" rename to "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" rename to "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" similarity index 100% rename from "\347\256\227\346\263\225/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" rename to "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" From cfd8ff460b93d386a7d9e6941aac9702cb86a782 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 29 Apr 2020 00:35:42 +0800 Subject: [PATCH 012/145] =?UTF-8?q?feat:=20=E5=9B=B4=E7=BB=95=E5=B2=9B?= =?UTF-8?q?=E5=B1=BF=E7=9A=84Github=20Issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7-\346\255\243\350\247\243\347\211\210.js" | 2 + ...25\347\232\204\345\214\272\345\237\237.js" | 78 +------------------ 2 files changed, 3 insertions(+), 77 deletions(-) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" => "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" (94%) diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" similarity index 94% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" rename to "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" index dce3133..f07ae19 100644 --- "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\347\256\200\346\264\201\347\211\210.js" +++ "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" @@ -1,3 +1,5 @@ +// https://github.com/sl1673495/daily-plan/issues/16 + var solve = function (board) { if (board.length == 0) return null; diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" index 55bc3f1..a425546 100644 --- "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" +++ "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" @@ -154,30 +154,6 @@ console.log( ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], ]; -// 输出 -[ - ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], - ["X", "X", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], - ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], - ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], - ["O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X"], - ["X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "X", "O", "O", "O", "O"], - ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], - ["X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], - ["X", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], - ["O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], - ["X", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "O", "O", "O"], - ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "X", "O"], - ["X", "O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O", "O", "X"], - ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O"], - ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O"], - ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X"], - ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "X"], - ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O"], - ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "X"], - ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], -]; - // 期望 [ ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], @@ -202,56 +178,4 @@ console.log( ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], ]; -[ - ["O", "X", "O", "O", "X", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "O", "O", "X", "O", "X"], - ["X", "X", "X", "O", "O", "X", "X", "O", "O", "X", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], - ["O", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X", "X", "X", "X", "O", "X"], - ["X", "X", "O", "O", "O", "X", "X", "O", "O", "O", "X", "X", "X", "O", "O", "X", "O", "X", "X", "O"], - ["O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "O", "O", "O", "X"], - ["X", "O", "O", "X", "X", "X", "O", "O", "O", "X", "X", "X", "X", "O", "O", "X", "O", "O", "O", "O"], - ["X", "O", "O", "O", "X", "X", "O", "O", "O", "O", "O", "X", "O", "O", "X", "O", "O", "O", "O", "X"], - ["X", "O", "O", "O", "X", "X", "X", "X", "X", "O", "X", "O", "X", "X", "X", "X", "O", "O", "O", "X"], - ["X", "O", "O", "X", "X", "X", "X", "X", "O", "O", "O", "O", "O", "O", "O", "O", "O", "X", "O", "X"], - ["O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O", "X", "O"], - ["X", "O", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "O", "O", "O"], - ["O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "O"], - ["X", "O", "O", "O", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O", "O", "X"], - ["O", "O", "O", "O", "X", "O", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O"], - ["O", "X", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "X", "O", "X", "O"], - ["X", "O", "X", "X", "X", "X", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X"], - ["X", "O", "O", "O", "X", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "X", "X", "X"], - ["O", "O", "X", "O", "O", "O", "O", "X", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "O"], - ["O", "O", "X", "O", "O", "O", "O", "O", "O", "X", "X", "X", "X", "X", "X", "O", "O", "O", "X", "X"], - ["X", "O", "O", "O", "X", "O", "X", "X", "X", "O", "O", "X", "O", "X", "O", "X", "X", "O", "O", "O"], -]; -// ;[ -// ["X", "X", "X", "X", "O", "O", "X", "X", "O"], -// ["O", "O", "O", "O", "X", "X", "O", "O", "X"], -// ["X", "O", "X", "O", "O", "X", "X", "O", "X"], -// ["O", "O", "X", "X", "X", "O", "O", "O", "O"], -// ["X", "O", "O", "X", "X", "X", "X", "X", "O"], -// ["O", "O", "X", "O", "X", "O", "X", "O", "X"], -// ["O", "O", "O", "X", "X", "O", "X", "O", "X"], -// ["O", "O", "O", "X", "O", "O", "O", "X", "O"], -// ["O", "X", "O", "O", "O", "X", "O", "X", "O"], -// ];[ -// (["X", "X", "X", "X", "O", "O", "X", "X", "O"], -// ["O", "O", "O", "O", "X", "X", "X", "X", "X"], -// ["X", "O", "X", "O", "O", "X", "X", "X", "X"], -// ["O", "O", "X", "X", "X", "X", "X", "X", "O"], -// ["X", "O", "O", "X", "X", "X", "X", "X", "O"], -// ["O", "O", "X", "X", "X", "O", "X", "X", "X"], -// ["O", "O", "O", "X", "X", "O", "X", "X", "X"], -// ["O", "O", "O", "X", "O", "O", "O", "X", "O"], -// ["O", "X", "O", "O", "O", "X", "O", "X", "O"]) -// ];[ -// (["X", "X", "X", "X", "O", "O", "X", "X", "O"], -// ["O", "O", "O", "O", "X", "X", "O", "O", "X"], -// ["X", "O", "X", "O", "O", "X", "X", "O", "X"], -// ["O", "O", "X", "X", "X", "O", "O", "O", "O"], -// ["X", "O", "O", "X", "X", "X", "X", "X", "O"], -// ["O", "O", "X", "X", "X", "O", "X", "X", "X"], -// ["O", "O", "O", "X", "X", "O", "X", "X", "X"], -// ["O", "O", "O", "X", "O", "O", "O", "X", "O"], -// ["O", "X", "O", "O", "O", "X", "O", "X", "O"]) -// ] +// 这个递归解法思路是对的,但是在最后一个测试用例爆栈了。 \ No newline at end of file From ff918a77bd96e7154e9856030bba616c7f4bd836 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 29 Apr 2020 12:10:50 +0800 Subject: [PATCH 013/145] =?UTF-8?q?feat:=20=E5=B2=9B=E5=B1=BF=E5=91=A8?= =?UTF-8?q?=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\232\204\345\221\250\351\225\277-463.js" | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" new file mode 100644 index 0000000..66508f7 --- /dev/null +++ "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" @@ -0,0 +1,68 @@ +// https://leetcode-cn.com/problems/island-perimeter +/** + * @param {number[][]} grid + * @return {number} + */ +var islandPerimeter = function (grid) { + let yLen = grid.length; + if (!yLen) return 0; + let xLen = grid[0].length; + + let perimeter = { value: 0 }; + + for (let y = 0; y < yLen; y++) { + for (let x = 0; x < xLen; x++) { + if (grid[y][x] === 1) { + dfs(grid, y, x, perimeter); + break; + } + } + } + + return perimeter.value; +}; + +function dfs(grid, y, x, perimeter) { + let cell = grid[y][x]; + if (cell === "COMPLETE") return; + + grid[y][x] = "COMPLETE"; + + let below = grid[y - 1] && grid[y - 1][x]; + let upper = grid[y + 1] && grid[y + 1][x]; + let left = grid[y][x - 1]; + let right = grid[y][x + 1]; + + if (below) { + dfs(grid, y - 1, x, perimeter); + } else { + perimeter.value++; + } + + if (upper) { + dfs(grid, y + 1, x, perimeter); + } else { + perimeter.value++; + } + + if (left) { + dfs(grid, y, x - 1, perimeter); + } else { + perimeter.value++; + } + + if (right) { + dfs(grid, y, x + 1, perimeter); + } else { + perimeter.value++; + } +} + +console.log( + islandPerimeter([ + [0, 1, 0, 0], + [1, 1, 1, 0], + [0, 1, 0, 0], + [1, 1, 0, 0], + ]) +); From 3f0b7e2ea7140638fd0e47d755c9052ebb1fbb7c Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 30 Apr 2020 11:52:54 +0800 Subject: [PATCH 014/145] =?UTF-8?q?feat:=20=E6=A0=91=E8=A1=8C=E4=B8=AD?= =?UTF-8?q?=E6=89=BE=E6=9C=80=E5=A4=A7=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\234\200\345\244\247\345\200\274-515.js" | 93 +++++++++++++++++++ ...45\203\217\346\270\262\346\237\223-733.js" | 38 ++++++++ 2 files changed, 131 insertions(+) create mode 100644 "\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" create mode 100644 "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" diff --git "a/\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" "b/\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" new file mode 100644 index 0000000..5a29d26 --- /dev/null +++ "b/\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" @@ -0,0 +1,93 @@ +/** + * 您需要在二叉树的每一行中找到最大的值。 + + 示例: + + 输入: + + 5 + / \ + 14 2 + / \ \ + 1 3 9 + + 输出: [1, 3, 9] + + 来源:力扣(LeetCode) + 链接:https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row + 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ + +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var largestValues = function (root) { + if (!root) return []; + let queue = [root]; + let maximums = []; + + while (queue.length) { + let max = Number.MIN_SAFE_INTEGER; + // 这里需要先缓存length 这个length代表当前层级的所有节点 + // 在循环开始后 会push新的节点 length就不稳定了 + let len = queue.length; + for (let i = 0; i < len; i++) { + let node = queue[i]; + max = Math.max(node.val, max); + + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } + } + + // 本「层级」处理完毕,截取掉。 + queue.splice(0, len); + + // 这个for循环结束后 代表当前层级的节点全部处理完毕 + // 直接把计算出来的最大值push到数组里即可。 + maximums.push(max); + } + + return maximums; +}; + +/** test case **/ +function TreeNode(val) { + this.val = val; + this.left = this.right = null; +} + +/** 预期:[1, 3, 9] */ +var root = new TreeNode(1); +root.left = new TreeNode(3); +root.right = new TreeNode(2); +root.left.left = new TreeNode(5); +root.left.right = new TreeNode(3); +root.right.right = new TreeNode(9); + +/** + 5 + / + 14 + / + 1 + + */ +/** 预期:[5, 14, 1] */ +var root2 = new TreeNode(5); +root2.left = new TreeNode(14); +root2.left.left = new TreeNode(1); + +console.log(largestValues(root)); +console.log(largestValues(root2)); diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" new file mode 100644 index 0000000..131d0e8 --- /dev/null +++ "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" @@ -0,0 +1,38 @@ +/** + * @param {number[][]} image + * @param {number} sr + * @param {number} sc + * @param {number} newColor + * @return {number[][]} + */ +var floodFill = function (image, sr, sc, newColor) { + let current = image[sr][sc]; + dfs(image, sr, sc, current, newColor); + return image; +}; + +function dfs(image, sr, sc, current, newColor) { + let row = image[sr]; + if (row === undefined) return; + + let cell = row[sc]; + if (cell === undefined || cell !== current || cell === newColor) return; + + row[sc] = newColor; + dfs(image, sr + 1, sc, current, newColor); + dfs(image, sr - 1, sc, current, newColor); + dfs(image, sr, sc + 1, current, newColor); + dfs(image, sr, sc - 1, current, newColor); +} + +console.log( + floodFill( + [ + [0, 0, 0], + [0, 0, 0], + ], + 0, + 0, + 2 + ) +); From 92265fc4f834f768bf1ea851e569296ecc27c050 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 30 Apr 2020 14:42:11 +0800 Subject: [PATCH 015/145] Update README.md --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 7036898..ab21de2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # frontend-code-fragment + 前端代码片段,想到什么写什么。 + +## 算法 + +力扣算法的一些记录,对于一些比较好的解法会尝试写文档。 + +## 算法-bobo 老师版本 + +跟着 bobo 老师的慕课网《LeetCode 真题》课程做一些记录。 + +## JavaScript 随笔 + +主要是一些 JavaScript 代码的记录。(不一定是原创,会标明出处) From a4289bf6bab64061fb0521d1a7b9e3d2499f212e Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 12:03:48 +0800 Subject: [PATCH 016/145] =?UTF-8?q?feat:=20=E6=89=BE=E7=A1=AC=E5=B8=81-?= =?UTF-8?q?=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1-\351\200\222\345\275\222\347\211\210.js" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" new file mode 100644 index 0000000..6324529 --- /dev/null +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" @@ -0,0 +1,31 @@ +// https://juejin.im/post/5e86d0ad6fb9a03c387f3342 + +/** + * 动态规划的前置思想,把问题拆分成子问题。 + * 比如f(15),可以拆分成min( + * f(4) + 11 * 1, + * f(10) + 5 * 1, + * f(14) + 1 * 1 + * ) + * 然后这里的f(4)、f(10)、f(14) 进一步按照这个步骤值拆分。 + * @param {number} n + */ +function f(n) { + if (n === 0) return 0; + let min = Infinity; + if (n >= 1) { + min = Math.min(f(n - 1) + 1, min); + } + + if (n >= 5) { + min = Math.min(f(n - 5) + 1, min); + } + + if (n >= 11) { + min = Math.min(f(n - 11) + 1, min); + } + + return min; +} + +console.log(f(15)); // 3 From 660b9ce8b9a8c471e082e1d483c4ecef341ce653 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 12:05:58 +0800 Subject: [PATCH 017/145] =?UTF-8?q?docs:=20=E6=89=BE=E7=A1=AC=E5=B8=81-?= =?UTF-8?q?=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" | 1 + 1 file changed, 1 insertion(+) diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" index 6324529..75051c2 100644 --- "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" @@ -8,6 +8,7 @@ * f(14) + 1 * 1 * ) * 然后这里的f(4)、f(10)、f(14) 进一步按照这个步骤值拆分。 + * 这种做法的缺点是容易爆栈。 * @param {number} n */ function f(n) { From fea59cd70dfa1067f538dc3cb8c0ec7c586f686f Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 12:11:24 +0800 Subject: [PATCH 018/145] =?UTF-8?q?refactor:=20=E5=B2=9B=E5=B1=BF=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=BD=92=E5=85=A5DFS=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\262\233\345\261\277\346\225\260\351\207\217.js" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" => "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" (100%) diff --git "a/\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" "b/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\262\233\345\261\277\346\225\260\351\207\217.js" rename to "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" From 4b6c50557c5ee9a9232ef889a0b4c05f25aaddca Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 13:20:30 +0800 Subject: [PATCH 019/145] =?UTF-8?q?feat:=20=E6=89=BE=E7=A1=AC=E5=B8=81-?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...06\351\200\222\345\275\222\347\211\210.js" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" new file mode 100644 index 0000000..c968038 --- /dev/null +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" @@ -0,0 +1,28 @@ +function f(n) { + function makeChange(amount) { + if(amount <= 0) return 0 + + // 校验是否已经在备忘录中存在结果,如果存在返回即可 + if(cache[amount]) return cache[amount] + + let min = Infinity + if (amount >= 1) { + min = Math.min(makeChange(amount-1) + 1, min) + } + + if (amount >= 5) { + min = Math.min(makeChange(amount-5) + 1, min) + } + + if (amount >= 11) { + min = Math.min(makeChange(amount-11) + 1, min) + } + + return (cache[amount] = min) + } + // 备忘录 + const cache = [] + return makeChange(n) +} + +console.log(f(705)) \ No newline at end of file From d19835b99a47c76500053ee41737002bbe0cb447 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 13:30:22 +0800 Subject: [PATCH 020/145] =?UTF-8?q?feat:=20=E6=89=BE=E7=A1=AC=E5=B8=81-?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=A7=84=E5=88=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\347\211\210.js" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" new file mode 100644 index 0000000..107865f --- /dev/null +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" @@ -0,0 +1,26 @@ +const coinChange = (coins, targetAmount) => { + // 初始化备忘录,用Infinity填满备忘录,Infinity说明该值不可以用硬币凑出来 + const dp = new Array(targetAmount + 1).fill(Infinity); + + // 设置初始条件为0 这一项无法用公式推导出来 + dp[0] = 0; + + for (let amount = 0; amount <= targetAmount; amount++) { + for (let j = 0; j < coins.length; j++) { + let coin = coins[j]; + if (coin <= amount) { + // 根据动态转移方程 求出当前面值需要的硬币数最小值 + dp[amount] = Math.min( + dp[amount], + // 比如目标15元 使用5元硬币 拆分为 dp(15 - 5) + 1 + dp[amount - coin] + 1 + ); + } + } + } + + // 如果 `dp[amount] === Infinity`说明没有最优解返回-1,否则返回最优解 + return dp[targetAmount] === Infinity ? -1 : dp[targetAmount]; +}; + +console.log(coinChange([1, 5, 11], 21)) From 1c79c28e3829df9efb3e4746caefb815f26f8282 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 20:33:46 +0800 Subject: [PATCH 021/145] =?UTF-8?q?feat:=20=E6=96=90=E6=B3=A2=E9=82=A3?= =?UTF-8?q?=E5=A5=91=E6=95=B0=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...01\350\247\204\345\210\222\347\211\210.js" | 2 +- ...45\245\221\346\225\260\345\210\227-509.js" | 16 +++++++ ...347\210\254\346\245\274\346\242\257-70.js" | 42 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" create mode 100644 "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" index 107865f..ce36dd1 100644 --- "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" @@ -5,7 +5,7 @@ const coinChange = (coins, targetAmount) => { // 设置初始条件为0 这一项无法用公式推导出来 dp[0] = 0; - for (let amount = 0; amount <= targetAmount; amount++) { + for (let amount = 1; amount <= targetAmount; amount++) { for (let j = 0; j < coins.length; j++) { let coin = coins[j]; if (coin <= amount) { diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" new file mode 100644 index 0000000..bdf4219 --- /dev/null +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" @@ -0,0 +1,16 @@ +/** + * @param {number} N + * @return {number} + */ +let fib = function (N) { + let dp = new Array(N + 1).fill(0); + + dp[0] = 0; + dp[1] = 1; + + for (let i = 2; i <= N; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[N]; +}; diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" new file mode 100644 index 0000000..05e1665 --- /dev/null +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" @@ -0,0 +1,42 @@ +/** + * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 + + 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? + + 注意:给定 n 是一个正整数。 + + 示例 1: + + 输入: 2 + 输出: 2 + 解释: 有两种方法可以爬到楼顶。 + 1. 1 阶 + 1 阶 + 2. 2 阶 + + + 来源:力扣(LeetCode) + 链接:https://leetcode-cn.com/problems/climbing-stairs + 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + */ + +/** + * 爬n阶楼梯,可以拆分为 + * 爬1阶加爬f(n-1)阶 + 爬2阶加爬f(n-2)阶 的方式。 + * @param {number} n + * @return {number} + */ +let climbStairs = function (n) { + let dp = new Array(n + 1).fill(0); + + // 初始条件 爬一阶只有一种方式 + dp[1] = 1; + dp[2] = 2 + + for (let i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; +}; + +console.log(climbStairs(3)) From e7302b1db0cace194fee2a32f6b6c52fde4dc9e1 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 1 May 2020 21:36:33 +0800 Subject: [PATCH 022/145] =?UTF-8?q?feat:=20=E5=8A=A8=E6=80=81=E8=A7=84?= =?UTF-8?q?=E5=88=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" | 2 +- ...241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" index c968038..785c067 100644 --- "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" @@ -25,4 +25,4 @@ function f(n) { return makeChange(n) } -console.log(f(705)) \ No newline at end of file +console.log(f(5000)) \ No newline at end of file diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" index 75051c2..f79b8e8 100644 --- "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" +++ "b/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" @@ -29,4 +29,4 @@ function f(n) { return min; } -console.log(f(15)); // 3 +console.log(f(50)); // 3 From e0ed6ccabb93115c1e575dbe9cd0adf3ffc185fb Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 2 May 2020 01:50:14 +0800 Subject: [PATCH 023/145] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ab21de2..cec08a5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# frontend-code-fragment +# leetcode-javascript -前端代码片段,想到什么写什么。 +力扣的一些解题记录和思路(issue)。 ## 算法 From 21217b12108c01f3c6bbc00f7e212b23e63c700e Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 2 May 2020 15:15:38 +0800 Subject: [PATCH 024/145] Update README.md --- README.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cec08a5..ef4bf9f 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,6 @@ 力扣的一些解题记录和思路(issue)。 -## 算法 +## 调试 -力扣算法的一些记录,对于一些比较好的解法会尝试写文档。 - -## 算法-bobo 老师版本 - -跟着 bobo 老师的慕课网《LeetCode 真题》课程做一些记录。 - -## JavaScript 随笔 - -主要是一些 JavaScript 代码的记录。(不一定是原创,会标明出处) +提供了 .vscode 配置文件,在 vscode 中选择「小爬虫」图标,点击启动程序,即可启动断点调试。 From 543ec9050407c4f6415fad4351d9c58d31b71e99 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 2 May 2020 15:16:12 +0800 Subject: [PATCH 025/145] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ef4bf9f..038ff2a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@ # leetcode-javascript -力扣的一些解题记录和思路(issue)。 +力扣的一些解题记录 ## 调试 提供了 .vscode 配置文件,在 vscode 中选择「小爬虫」图标,点击启动程序,即可启动断点调试。 + +## 思路 + +思路会记录在本仓库的 Issues 中,按照 label 进行分类。 From 571d8f3ff3de0ef98ec0866cbddba5e7a0cb69f2 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 2 May 2020 15:18:26 +0800 Subject: [PATCH 026/145] refactor: flat dir --- ...46\234\200\345\244\247\345\200\274-515.js" | 0 .../index.md" | 0 ...45\203\217\346\270\262\346\237\223-733.js" | 0 ...33\345\261\277\346\225\260\351\207\217.js" | 0 ...47\232\204\345\221\250\351\225\277-463.js" | 0 ...45\244\247\351\235\242\347\247\257-659.js" | 0 ...7-\345\276\252\347\216\257\347\211\210.js" | 0 ...7-\346\255\243\350\247\243\347\211\210.js" | 0 ...25\347\232\204\345\214\272\345\237\237.js" | 0 .../async-to-generator.js" | 65 ------ .../combine-skus.js" | 23 -- .../promise-easy.js" | 42 ---- .../promise.js" | 202 ------------------ .../scroll.js" | 48 ----- .../longest-prefix.js" => longest-prefix.js | 0 ...-four-divisors.js" => sum-four-divisors.js | 0 ...11\346\225\260\344\271\213\345\222\214.js" | 0 ...14\345\210\206\346\237\245\346\211\276.js" | 4 + ... "\344\272\214\345\217\211\346\240\221.js" | 0 ...16\345\272\217\351\201\215\345\216\206.js" | 0 ...30\351\242\221\345\205\203\347\264\240.js" | 0 ...01\350\247\204\345\210\222\347\211\210.js" | 0 ...06\351\200\222\345\275\222\347\211\210.js" | 0 ...1-\351\200\222\345\275\222\347\211\210.js" | 0 ...45\245\221\346\225\260\345\210\227-509.js" | 0 ...347\210\254\346\245\274\346\242\257-70.js" | 0 ...74\344\272\214\345\217\211\346\240\221.js" | 0 ...11\345\272\217\346\225\260\347\273\204.js" | 0 ...11\345\272\217\351\223\276\350\241\250.js" | 0 ...00\347\237\255\350\267\257\345\276\204.js" | 0 ...30\345\205\210\351\201\215\345\216\206.js" | 0 ...22\345\271\266\346\216\222\345\272\217.js" | 0 ...347\232\204\344\275\215\347\275\256-28.js" | 0 ...50\347\252\227\345\217\243\357\274\211.js" | 0 ...54\345\205\261\345\211\215\347\274\200.js" | 0 ...04\346\261\202\344\272\244\351\233\206.js" | 0 ...44\270\252\345\205\203\347\264\240-540.js" | 0 ...07\351\242\230\345\210\206\347\273\204.js" | 0 ...74\351\233\267\347\274\226\347\240\201.js" | 0 ...15\350\212\261\351\227\256\351\242\230.js" | 0 ...47\247\273\345\212\250\351\233\266-283.js" | 0 ...73\351\231\244\345\205\203\347\264\240.js" | 0 ...14\345\210\206\346\237\245\346\211\276.js" | 23 -- ...54\344\272\214\345\217\211\346\240\221.js" | 0 ...7\345\255\227\347\254\246\344\270\2622.js" | 0 ...01\345\255\220\345\272\217\345\210\227.js" | 0 ...41\344\272\214\345\217\211\346\240\221.js" | 0 47 files changed, 4 insertions(+), 403 deletions(-) rename "\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" => "BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/index.md" => "DFS\351\227\256\351\242\230/index.md" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" => "DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" => "DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" => "DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" => "DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" => "DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" => "DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" (100%) rename "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" => "DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" (100%) delete mode 100644 "JavaScript\351\232\217\347\254\224/async-to-generator.js" delete mode 100644 "JavaScript\351\232\217\347\254\224/combine-skus.js" delete mode 100644 "JavaScript\351\232\217\347\254\224/promise-easy.js" delete mode 100644 "JavaScript\351\232\217\347\254\224/promise.js" delete mode 100644 "JavaScript\351\232\217\347\254\224/scroll.js" rename "\347\256\227\346\263\225/longest-prefix.js" => longest-prefix.js (100%) rename "\347\256\227\346\263\225/sum-four-divisors.js" => sum-four-divisors.js (100%) rename "\347\256\227\346\263\225/\344\270\211\346\225\260\344\271\213\345\222\214.js" => "\344\270\211\346\225\260\344\271\213\345\222\214.js" (100%) rename "\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" => "\344\272\214\345\210\206\346\237\245\346\211\276.js" (86%) rename "\347\256\227\346\263\225/\344\272\214\345\217\211\346\240\221.js" => "\344\272\214\345\217\211\346\240\221.js" (100%) rename "\347\256\227\346\263\225/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\344\270\255\345\220\216\345\272\217\351\201\215\345\216\206.js" => "\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\344\270\255\345\220\216\345\272\217\351\201\215\345\216\206.js" (100%) rename "\347\256\227\346\263\225/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" => "\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" (100%) rename "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" => "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" (100%) rename "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" => "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" (100%) rename "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" => "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" (100%) rename "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" => "\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" (100%) rename "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" => "\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" (100%) rename "\347\256\227\346\263\225/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" => "\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" (100%) rename "\347\256\227\346\263\225/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" => "\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" (100%) rename "\347\256\227\346\263\225/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" => "\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" (100%) rename "\347\256\227\346\263\225/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" => "\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" (100%) rename "\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" => "\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" (100%) rename "\347\256\227\346\263\225/\345\275\222\345\271\266\346\216\222\345\272\217.js" => "\345\275\222\345\271\266\346\216\222\345\272\217.js" (100%) rename "\347\256\227\346\263\225/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" => "\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" (100%) rename "\347\256\227\346\263\225/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" => "\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" (100%) rename "\347\256\227\346\263\225/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" => "\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" (100%) rename "\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" => "\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" (100%) rename "\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" => "\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" (100%) rename "\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" => "\346\240\207\351\242\230\345\210\206\347\273\204.js" (100%) rename "\347\256\227\346\263\225/\346\240\274\351\233\267\347\274\226\347\240\201.js" => "\346\240\274\351\233\267\347\274\226\347\240\201.js" (100%) rename "\347\256\227\346\263\225/\347\247\215\350\212\261\351\227\256\351\242\230.js" => "\347\247\215\350\212\261\351\227\256\351\242\230.js" (100%) rename "\347\256\227\346\263\225/\347\247\273\345\212\250\351\233\266-283.js" => "\347\247\273\345\212\250\351\233\266-283.js" (100%) rename "\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" => "\347\247\273\351\231\244\345\205\203\347\264\240.js" (100%) delete mode 100644 "\347\256\227\346\263\225/\344\272\214\345\210\206\346\237\245\346\211\276.js" rename "\347\256\227\346\263\225/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" => "\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" (100%) rename "\347\256\227\346\263\225/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" => "\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" (100%) rename "\347\256\227\346\263\225/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" => "\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" (100%) rename "\347\256\227\346\263\225/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" => "\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" (100%) diff --git "a/\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" "b/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" similarity index 100% rename from "\347\256\227\346\263\225/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" rename to "BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/index.md" "b/DFS\351\227\256\351\242\230/index.md" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/index.md" rename to "DFS\351\227\256\351\242\230/index.md" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" "b/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" rename to "DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" rename to "DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" rename to "DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" rename to "DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" rename to "DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" rename to "DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" diff --git "a/\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" similarity index 100% rename from "\347\256\227\346\263\225/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" rename to "DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" diff --git "a/JavaScript\351\232\217\347\254\224/async-to-generator.js" "b/JavaScript\351\232\217\347\254\224/async-to-generator.js" deleted file mode 100644 index 4d3d2d4..0000000 --- "a/JavaScript\351\232\217\347\254\224/async-to-generator.js" +++ /dev/null @@ -1,65 +0,0 @@ -/** - * async的执行原理 - * 其实就是自动执行generator函数 - * 暂时不考虑genertor的编译步骤(更复杂) - */ - -const getData = () => - new Promise(resolve => setTimeout(() => resolve("data"), 1000)) - -// 这样的一个async函数 应该再1秒后打印data -async function test() { - const data = await getData() - console.log('data: ', data); - const data2 = await getData() - console.log('data2: ', data2); - return 'success' -} - -// async函数会被编译成generator函数 (babel会编译成更本质的形态,这里我们直接用generator) -function* testG() { - // await被编译成了yield - const data = yield getData() - console.log('data: ', data); - const data2 = yield getData() - console.log('data2: ', data2); - return 'success' -} - -function asyncToGenerator(generatorFunc) { - return function() { - const gen = generatorFunc.apply(this, arguments) - - return new Promise((resolve, reject) => { - function step(key, arg) { - let generatorResult - try { - generatorResult = gen[key](arg) - } catch (error) { - return reject(error) - } - - const { value, done } = generatorResult - - if (done) { - return resolve(value) - } else { - return Promise.resolve(value).then( - function onResolve(val) { - step("next", val) - }, - function onReject(err) { - step("throw", err) - }, - ) - } - } - step("next") - }) - } -} - -const testGAsync = asyncToGenerator(testG) -testGAsync().then(result => { - console.log(result) -}) diff --git "a/JavaScript\351\232\217\347\254\224/combine-skus.js" "b/JavaScript\351\232\217\347\254\224/combine-skus.js" deleted file mode 100644 index 8d2482f..0000000 --- "a/JavaScript\351\232\217\347\254\224/combine-skus.js" +++ /dev/null @@ -1,23 +0,0 @@ -var s1 = ['大陆', '港版', '日版'] -var s2 = ['64g', '32g'] -var s3 = ['金色', '黄色', '蓝色'] -var s4 = ['标准套餐1', '标准套餐2'] - -var combind = (...arrs) => { - var [head, ...rest] = arrs - var backet = [...head] - var _combine = (backet, ...rest) => { - const results = [] - const [head, ...more] = rest - if (!head) return backet - head.forEach(item1 => { - backet.forEach(item2 => { - results.push(`${item2}-${item1}`) - }) - }) - return _combine(results, ...more) - } - return _combine(backet, ...rest) -} - -console.log(combind(s1, s2, s3, s4)) \ No newline at end of file diff --git "a/JavaScript\351\232\217\347\254\224/promise-easy.js" "b/JavaScript\351\232\217\347\254\224/promise-easy.js" deleted file mode 100644 index 75b20a2..0000000 --- "a/JavaScript\351\232\217\347\254\224/promise-easy.js" +++ /dev/null @@ -1,42 +0,0 @@ -/** - * 仅实现异步链式调用的简化版 - */ -function Promise(excutor) { - var self = this - self.onResolvedCallback = [] // Promise resolve时的回调函数集,因为在Promise结束之前有可能有多个回调添加到它上面 - function resolve(value) { - self.data = value - self.onResolvedCallback.forEach(callback => callback(value)) - } - excutor(resolve.bind(self)) -} -Promise.prototype.then = function(onResolved) { - var self = this - return new Promise(resolve => { - self.onResolvedCallback.push(function() { - var result = onResolved(self.data) - if (result instanceof Promise) { - result.then(resolve) - } else { - resolve(result) - } - }) - }) -} - -new Promise(resolve => { - setTimeout(() => { - resolve(1) - }, 500) -}) - .then(res => { - console.log(res) - return new Promise(resolve => { - setTimeout(() => { - resolve(2) - }, 500) - }) - }) - .then(console.log) - - \ No newline at end of file diff --git "a/JavaScript\351\232\217\347\254\224/promise.js" "b/JavaScript\351\232\217\347\254\224/promise.js" deleted file mode 100644 index 858ea57..0000000 --- "a/JavaScript\351\232\217\347\254\224/promise.js" +++ /dev/null @@ -1,202 +0,0 @@ -// https://github.com/xieranmaya/blog/issues/3 - -function Promise(executor) { - var self = this - self.status = "pending" // Promise当前的状态 - self.data = undefined // Promise的值 - self.onResolvedCallback = [] // Promise resolve时的回调函数集,因为在Promise结束之前有可能有多个回调添加到它上面 - self.onRejectedCallback = [] // Promise reject时的回调函数集,因为在Promise结束之前有可能有多个回调添加到它上面 - - function resolve(value) { - if (self.status === "pending") { - self.status = "resolved" - self.data = value - for (var i = 0; i < self.onResolvedCallback.length; i++) { - self.onResolvedCallback[i](value) - } - } - } - - function reject(reason) { - if (self.status === "pending") { - self.status = "rejected" - self.data = reason - for (var i = 0; i < self.onRejectedCallback.length; i++) { - self.onRejectedCallback[i](reason) - } - } - } - - try { - // 考虑到执行executor的过程中有可能出错,所以我们用try/catch块给包起来,并且在出错后以catch到的值reject掉这个Promise - executor(resolve, reject) // 执行executor - } catch (e) { - reject(e) - } -} -Promise.prototype.then = function(onResolved, onRejected) { - var self = this - var promise2 - - // 根据标准,如果then的参数不是function,则我们需要忽略它,此处以如下方式处理 - // 并且默认函数需要return值 以便于空值的穿透 - onResolved = - typeof onResolved === "function" - ? onResolved - : function(value) { - return value - } - onRejected = - typeof onRejected === "function" - ? onRejected - : function(reason) { - return reason - } - - if (self.status === "resolved") { - // 如果promise1(此处即为this/self)的状态已经确定并且是resolved,我们调用onResolved - // 因为考虑到有可能throw,所以我们将其包在try/catch块里 - return (promise2 = new Promise(function(resolve, reject) { - try { - var x = onResolved(self.data) - resolvePromise(promise2, x, resolve, reject) - } catch (e) { - reject(e) // 如果出错,以捕获到的错误做为promise2的结果 - } - })) - } - - // 此处与前一个if块的逻辑几乎相同,区别在于所调用的是onRejected函数,就不再做过多解释 - if (self.status === "rejected") { - return (promise2 = new Promise(function(resolve, reject) { - try { - var x = onRejected(self.data) - resolvePromise(promise2, x, resolve, reject) - } catch (e) { - reject(e) - } - })) - } - - if (self.status === "pending") { - // 如果当前的Promise还处于pending状态,我们并不能确定调用onResolved还是onRejected, - // 只能等到Promise的状态确定后,才能确实如何处理。 - // 所以我们需要把我们的**两种情况**的处理逻辑做为callback放入promise1(此处即this/self)的回调数组里 - // 逻辑本身跟第一个if块内的几乎一致,此处不做过多解释 - return (promise2 = new Promise(function(resolve, reject) { - self.onResolvedCallback.push(function(value) { - try { - var x = onResolved(self.data) - resolvePromise(promise2, x, resolve, reject) - } catch (e) { - reject(e) - } - }) - - self.onRejectedCallback.push(function(reason) { - try { - var x = onRejected(self.data) - resolvePromise(promise2, x, resolve, reject) - } catch (e) { - reject(e) - } - }) - })) - } -} - -/* -resolvePromise函数即为根据x的值来决定promise2的状态的函数 -也即标准中的[Promise Resolution Procedure](https://promisesaplus.com/#point-47) -x为`promise2 = promise1.then(onResolved, onRejected)`里`onResolved/onRejected`的返回值 -`resolve`和`reject`实际上是`promise2`的`executor`的两个实参,因为很难挂在其它的地方,所以一并传进来。 -相信各位一定可以对照标准把标准转换成代码,这里就只标出代码在标准中对应的位置,只在必要的地方做一些解释 -*/ -function resolvePromise(promise2, x, resolve, reject) { - var then - var thenCalledOrThrow = false - - // 对应标准2.3.1节 - // If promise and x refer to the same object, reject promise with a TypeError as the reason. - if (promise2 === x) { - return reject(new TypeError("Chaining cycle detected for promise!")) - } - - // 2.3.2 - /** - * If x is a promise, adopt its state [3.4]: - If x is pending, promise must remain pending until x is fulfilled or rejected. - If/when x is fulfilled, fulfill promise with the same value. - If/when x is rejected, reject promise with the same reason. - */ - - if (x instanceof Promise) { - // 对应标准2.3.2节 - // 如果x的状态还没有确定,那么它是有可能被一个thenable决定最终状态和值的 有可能x是个promise但是它resolve的又是一个promise - // 比如 - // 所以这里需要做一下处理,而不能一概的以为它会被一个“正常”的值resolve - if (x.status === "pending") { - x.then(function(value) { - resolvePromise(promise2, value, resolve, reject) - }, reject) - } else { - // 但如果这个Promise的状态已经确定了,那么它肯定有一个“正常”的值,而不是一个thenable,所以这里直接取它的状态 - x.then(resolve, reject) - } - return - } - - if (x !== null && (typeof x === "object" || typeof x === "function")) { - // 2.3.3 - try { - // 2.3.3.1 因为x.then有可能是一个getter,这种情况下多次读取就有可能产生副作用 - // 即要判断它的类型,又要调用它,这就是两次读取 - then = x.then - - if (typeof then === "function") { - // 2.3.3.3 - then.call( - x, - function rs(y) { - // 2.3.3.3.1 - if (thenCalledOrThrow) return - thenCalledOrThrow = true - return resolvePromise(promise2, y, resolve, reject) // 2.3.3.3.1 - }, - function rj(r) { - if (thenCalledOrThrow) return // 2.3.3.3.3 即这三处谁选执行就以谁的结果为准 - thenCalledOrThrow = true - return reject(r) - }, - ) - } else { - resolve(x) - } - } catch (e) { - if (thenCalledOrThrow) return // 2.3.3.3.3 即这三处谁选执行就以谁的结果为准 - thenCalledOrThrow = true - return reject(e) - } - } else { - resolve(x) - } -} - -var a = new Promise(resolve => { - setTimeout(() => { - resolve(1) - }, 500) -}) - .then(res => { - console.log("res1", res) - return new Promise(r => { - r( - new Promise(r1 => { - r1(15) - }), - ) - }) - }) - .then(res => { - console.log("res2", res) - }) diff --git "a/JavaScript\351\232\217\347\254\224/scroll.js" "b/JavaScript\351\232\217\347\254\224/scroll.js" deleted file mode 100644 index 7ae2acd..0000000 --- "a/JavaScript\351\232\217\347\254\224/scroll.js" +++ /dev/null @@ -1,48 +0,0 @@ -export const raf = (() => { - return window.requestAnimationFrame || - window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - window.oRequestAnimationFrame || - window.msRequestAnimationFrame || - function (callback) { - window.setTimeout(callback, 1000 / 60) - } -})() - -let isScrolling = false -let stop = false - -export function scroll (el, type, targetPos, time, next) { - if (isScrolling) { - stop = true - } - isScrolling = true - const start = Date.now() - const currentPos = el[type] - - raf(step) - - function step () { - const current = Date.now() - const rate = Math.min(1, (current - start) / time) - if (rate === 1) { - el[type] = targetPos - isScrolling = false - next && next() - } else { - el[type] = currentPos + (targetPos - currentPos) * rate - if (!stop) { - raf(step) - } else { - stop = false - } - } - } -} - -document.addEventListener('visibilitychange', () => { - if (!document.hidden) { - isScrolling = false - stop = false - } -}) \ No newline at end of file diff --git "a/\347\256\227\346\263\225/longest-prefix.js" b/longest-prefix.js similarity index 100% rename from "\347\256\227\346\263\225/longest-prefix.js" rename to longest-prefix.js diff --git "a/\347\256\227\346\263\225/sum-four-divisors.js" b/sum-four-divisors.js similarity index 100% rename from "\347\256\227\346\263\225/sum-four-divisors.js" rename to sum-four-divisors.js diff --git "a/\347\256\227\346\263\225/\344\270\211\346\225\260\344\271\213\345\222\214.js" "b/\344\270\211\346\225\260\344\271\213\345\222\214.js" similarity index 100% rename from "\347\256\227\346\263\225/\344\270\211\346\225\260\344\271\213\345\222\214.js" rename to "\344\270\211\346\225\260\344\271\213\345\222\214.js" diff --git "a/\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" "b/\344\272\214\345\210\206\346\237\245\346\211\276.js" similarity index 86% rename from "\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" rename to "\344\272\214\345\210\206\346\237\245\346\211\276.js" index 04e5f42..2c29806 100644 --- "a/\347\256\227\346\263\225-bobo\350\200\201\345\270\210\347\211\210\346\234\254/\344\272\214\345\210\206\346\237\245\346\211\276.js" +++ "b/\344\272\214\345\210\206\346\237\245\346\211\276.js" @@ -1,3 +1,7 @@ +/** + * 二分搜索 从一个数组中搜索一个确定的数 + */ + function binarySearch(arr, n, target) { // 在[l...r]的范围里寻找target let l = 0; diff --git "a/\347\256\227\346\263\225/\344\272\214\345\217\211\346\240\221.js" "b/\344\272\214\345\217\211\346\240\221.js" similarity index 100% rename from "\347\256\227\346\263\225/\344\272\214\345\217\211\346\240\221.js" rename to "\344\272\214\345\217\211\346\240\221.js" diff --git "a/\347\256\227\346\263\225/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\344\270\255\345\220\216\345\272\217\351\201\215\345\216\206.js" "b/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\344\270\255\345\220\216\345\272\217\351\201\215\345\216\206.js" similarity index 100% rename from "\347\256\227\346\263\225/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\344\270\255\345\220\216\345\272\217\351\201\215\345\216\206.js" rename to "\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\344\270\255\345\220\216\345\272\217\351\201\215\345\216\206.js" diff --git "a/\347\256\227\346\263\225/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" "b/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" rename to "\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\345\212\250\346\200\201\350\247\204\345\210\222\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\351\200\222\345\275\222\347\211\210.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" diff --git "a/\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\347\210\254\346\245\274\346\242\257-70.js" diff --git "a/\347\256\227\346\263\225/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" "b/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" rename to "\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" diff --git "a/\347\256\227\346\263\225/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" "b/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" rename to "\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" diff --git "a/\347\256\227\346\263\225/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" "b/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" rename to "\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" diff --git "a/\347\256\227\346\263\225/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" "b/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" rename to "\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" diff --git "a/\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" "b/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" rename to "\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\345\222\214\346\267\261\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206.js" diff --git "a/\347\256\227\346\263\225/\345\275\222\345\271\266\346\216\222\345\272\217.js" "b/\345\275\222\345\271\266\346\216\222\345\272\217.js" similarity index 100% rename from "\347\256\227\346\263\225/\345\275\222\345\271\266\346\216\222\345\272\217.js" rename to "\345\275\222\345\271\266\346\216\222\345\272\217.js" diff --git "a/\347\256\227\346\263\225/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" "b/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" rename to "\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" diff --git "a/\347\256\227\346\263\225/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" "b/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" rename to "\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" diff --git "a/\347\256\227\346\263\225/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" "b/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" rename to "\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" diff --git "a/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" "b/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" rename to "\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" diff --git "a/\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" "b/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" rename to "\346\234\211\345\272\217\346\225\260\347\273\204\347\232\204\345\215\225\344\270\252\345\205\203\347\264\240-540.js" diff --git "a/\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" "b/\346\240\207\351\242\230\345\210\206\347\273\204.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\240\207\351\242\230\345\210\206\347\273\204.js" rename to "\346\240\207\351\242\230\345\210\206\347\273\204.js" diff --git "a/\347\256\227\346\263\225/\346\240\274\351\233\267\347\274\226\347\240\201.js" "b/\346\240\274\351\233\267\347\274\226\347\240\201.js" similarity index 100% rename from "\347\256\227\346\263\225/\346\240\274\351\233\267\347\274\226\347\240\201.js" rename to "\346\240\274\351\233\267\347\274\226\347\240\201.js" diff --git "a/\347\256\227\346\263\225/\347\247\215\350\212\261\351\227\256\351\242\230.js" "b/\347\247\215\350\212\261\351\227\256\351\242\230.js" similarity index 100% rename from "\347\256\227\346\263\225/\347\247\215\350\212\261\351\227\256\351\242\230.js" rename to "\347\247\215\350\212\261\351\227\256\351\242\230.js" diff --git "a/\347\256\227\346\263\225/\347\247\273\345\212\250\351\233\266-283.js" "b/\347\247\273\345\212\250\351\233\266-283.js" similarity index 100% rename from "\347\256\227\346\263\225/\347\247\273\345\212\250\351\233\266-283.js" rename to "\347\247\273\345\212\250\351\233\266-283.js" diff --git "a/\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" "b/\347\247\273\351\231\244\345\205\203\347\264\240.js" similarity index 100% rename from "\347\256\227\346\263\225/\347\247\273\351\231\244\345\205\203\347\264\240.js" rename to "\347\247\273\351\231\244\345\205\203\347\264\240.js" diff --git "a/\347\256\227\346\263\225/\344\272\214\345\210\206\346\237\245\346\211\276.js" "b/\347\256\227\346\263\225/\344\272\214\345\210\206\346\237\245\346\211\276.js" deleted file mode 100644 index 19b74e5..0000000 --- "a/\347\256\227\346\263\225/\344\272\214\345\210\206\346\237\245\346\211\276.js" +++ /dev/null @@ -1,23 +0,0 @@ -/** - * 二分搜索 从一个数组中搜索一个确定的数 - */ - -var search = function(nums, target, offset = 0) { - let medium = Math.floor(nums.length / 2) - let mediumVal = nums[medium] - let left = nums.slice(0, medium) - let right = nums.slice(medium, nums.length) - - if (nums.length === 1 && mediumVal !== target) { - return -1 - } - - if (mediumVal > target) { - return search(left, target, offset) - } else if (mediumVal < target) { - return search(right, target, offset + left.length) - } else if (mediumVal === target) { - return medium + offset - } -} -console.log(search([1, 2, 3, 4, 5], 2)) diff --git "a/\347\256\227\346\263\225/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" "b/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" similarity index 100% rename from "\347\256\227\346\263\225/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" rename to "\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" diff --git "a/\347\256\227\346\263\225/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" "b/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" similarity index 100% rename from "\347\256\227\346\263\225/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" rename to "\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" diff --git "a/\347\256\227\346\263\225/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" "b/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" similarity index 100% rename from "\347\256\227\346\263\225/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" rename to "\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" diff --git "a/\347\256\227\346\263\225/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" "b/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" similarity index 100% rename from "\347\256\227\346\263\225/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" rename to "\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" From 154af66b3dd4ee48527d019715a96d6498748afc Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 3 May 2020 01:02:12 +0800 Subject: [PATCH 027/145] =?UTF-8?q?feat:=20=E5=AE=8C=E5=85=A8=E5=B9=B3?= =?UTF-8?q?=E6=96=B9=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\271\263\346\226\271\346\225\260-279.js" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" new file mode 100644 index 0000000..35c760f --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" @@ -0,0 +1,33 @@ +// https://github.com/sl1673495/leetcode-javascript/issues/9 + +/** + * @param {number} n + * @return {number} + */ +let numSquares = function (n) { + let dp = []; + + // 求0就假设为0次 + dp[0] = 0; + + for (let i = 1; i <= n; i++) { + let j = 1; + // 初始化为Infinity 这样后面任意一个小值都可以覆盖它 + let min = Infinity; + while (true) { + // 用 i 减去不断递增的平方数 j * j + let prev = i - j * j; + if (prev < 0) { + break; + } + + // 假设i = 10、j = 1 实际上就是在求dp[10 - 1] + 1 + // 也就是凑成 9 的最小次数 再加上 1(也就是 1 这个平方数的次数) + min = Math.min(min, dp[prev] + 1); + j++; + } + dp[i] = min === Infinity ? 0 : min; + } + + return dp[n]; +}; From af20cf16690702b303b447fd521ac84cce3ccf42 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 3 May 2020 13:16:39 +0800 Subject: [PATCH 028/145] feat: BigInt fib --- ...45\245\221\346\225\260\345\210\227-509.js" | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" index bdf4219..af64b5d 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" @@ -3,10 +3,10 @@ * @return {number} */ let fib = function (N) { - let dp = new Array(N + 1).fill(0); + let dp = [] - dp[0] = 0; - dp[1] = 1; + dp[0] = 0n; + dp[1] = 1n; for (let i = 2; i <= N; i++) { dp[i] = dp[i - 1] + dp[i - 2]; @@ -14,3 +14,30 @@ let fib = function (N) { return dp[N]; }; + +// let fib = (function () { +// let memo = new Map(); +// return function (n) { +// // 优先从记忆里取 找到就直接 return +// // 否则又要进入下面的递归逻辑 非常耗时 +// let memorized = memo.get(n); +// if (memorized) { +// return memorized; +// } + +// if (n == 1 || n == 2) { +// return 1; +// } + +// let f1 = fib(n - 1) +// let f2 = fib(n - 2) + +// // 记忆下来 +// memo.set(n - 1, f1) +// memo.set(n - 2, f2) + +// return f1 + f2 +// }; +// })(); + +console.log(fib(10000)); From 44d9c2e339f562b8ed28fe722e2dd5b1b6900910 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 3 May 2020 13:24:32 +0800 Subject: [PATCH 029/145] refactor: var -> let --- ...46\234\200\345\244\247\345\200\274-515.js" | 6 ++-- "DFS\351\227\256\351\242\230/index.md" | 10 +++---- ...45\203\217\346\270\262\346\237\223-733.js" | 2 +- ...33\345\261\277\346\225\260\351\207\217.js" | 2 +- ...47\232\204\345\221\250\351\225\277-463.js" | 2 +- ...45\244\247\351\235\242\347\247\257-659.js" | 2 +- ...7-\345\276\252\347\216\257\347\211\210.js" | 2 +- ...7-\346\255\243\350\247\243\347\211\210.js" | 10 +++---- ...25\347\232\204\345\214\272\345\237\237.js" | 2 +- sum-four-divisors.js | 16 +++++----- ...11\346\225\260\344\271\213\345\222\214.js" | 12 ++++---- ...30\351\242\221\345\205\203\347\264\240.js" | 16 +++++----- ...74\344\272\214\345\217\211\346\240\221.js" | 10 +++---- ...351\207\215\345\244\215\351\241\271-26.js" | 29 +++++++++++++++++++ ...11\345\272\217\346\225\260\347\273\204.js" | 4 +-- ...11\345\272\217\351\223\276\350\241\250.js" | 12 ++++---- ...00\347\237\255\350\267\257\345\276\204.js" | 20 ++++++------- ...22\345\271\266\346\216\222\345\272\217.js" | 22 +++++++------- ...347\232\204\344\275\215\347\275\256-28.js" | 10 +++---- ...50\347\252\227\345\217\243\357\274\211.js" | 16 +++++----- ...54\345\205\261\345\211\215\347\274\200.js" | 14 ++++----- ...04\346\261\202\344\272\244\351\233\206.js" | 18 ++++++------ ...74\351\233\267\347\274\226\347\240\201.js" | 2 +- ...15\350\212\261\351\227\256\351\242\230.js" | 10 +++---- "\347\247\273\345\212\250\351\233\266-283.js" | 8 ++--- ...73\351\231\244\345\205\203\347\264\240.js" | 2 +- ...54\344\272\214\345\217\211\346\240\221.js" | 8 ++--- ...7\345\255\227\347\254\246\344\270\2622.js" | 6 ++-- ...01\345\255\220\345\272\217\345\210\227.js" | 18 ++++++------ ...41\344\272\214\345\217\211\346\240\221.js" | 4 +-- 30 files changed, 162 insertions(+), 133 deletions(-) create mode 100644 "\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" diff --git "a/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" "b/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" index 5a29d26..59e7d54 100644 --- "a/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" +++ "b/BFS\351\227\256\351\242\230/\345\234\250\346\257\217\344\270\252\346\240\221\350\241\214\344\270\255\346\211\276\346\234\200\345\244\247\345\200\274-515.js" @@ -29,7 +29,7 @@ * @param {TreeNode} root * @return {number[]} */ -var largestValues = function (root) { +let largestValues = function (root) { if (!root) return []; let queue = [root]; let maximums = []; @@ -69,7 +69,7 @@ function TreeNode(val) { } /** 预期:[1, 3, 9] */ -var root = new TreeNode(1); +let root = new TreeNode(1); root.left = new TreeNode(3); root.right = new TreeNode(2); root.left.left = new TreeNode(5); @@ -85,7 +85,7 @@ root.right.right = new TreeNode(9); */ /** 预期:[5, 14, 1] */ -var root2 = new TreeNode(5); +let root2 = new TreeNode(5); root2.left = new TreeNode(14); root2.left.left = new TreeNode(1); diff --git "a/DFS\351\227\256\351\242\230/index.md" "b/DFS\351\227\256\351\242\230/index.md" index acd4db2..55b6081 100644 --- "a/DFS\351\227\256\351\242\230/index.md" +++ "b/DFS\351\227\256\351\242\230/index.md" @@ -9,19 +9,19 @@ 这样遍历所走的路就会少很多。 ```js -var solve = function (board) { +let solve = function (board) { if (board.length == 0) return null; - for (var y = 0; y < board.length; y++) { - for (var x = 0; x < board[0].length; x++) { + for (let y = 0; y < board.length; y++) { + for (let x = 0; x < board[0].length; x++) { if (board[y][x] == "O" && (y == 0 || y == board.length - 1 || x == 0 || x == board[0].length - 1)) { dfs(board, y, x); } } } - for (var y = 0; y < board.length; y++) { - for (var x = 0; x < board[0].length; x++) { + for (let y = 0; y < board.length; y++) { + for (let x = 0; x < board[0].length; x++) { if (board[y][x] == "W") { board[y][x] = "O"; } else { diff --git "a/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" "b/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" index 131d0e8..2f554ad 100644 --- "a/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" +++ "b/DFS\351\227\256\351\242\230/\345\233\276\345\203\217\346\270\262\346\237\223-733.js" @@ -5,7 +5,7 @@ * @param {number} newColor * @return {number[][]} */ -var floodFill = function (image, sr, sc, newColor) { +let floodFill = function (image, sr, sc, newColor) { let current = image[sr][sc]; dfs(image, sr, sc, current, newColor); return image; diff --git "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" index 02fdba3..0d22d39 100644 --- "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" +++ "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\346\225\260\351\207\217.js" @@ -26,7 +26,7 @@ 链接:https://leetcode-cn.com/problems/number-of-islands 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 */ -var numIslands = function (grid) { +let numIslands = function (grid) { let count = 0 for (let i = 0; i < grid.length; i++) { let row = grid[i] diff --git "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" index 66508f7..5586d3d 100644 --- "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" +++ "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\345\221\250\351\225\277-463.js" @@ -3,7 +3,7 @@ * @param {number[][]} grid * @return {number} */ -var islandPerimeter = function (grid) { +let islandPerimeter = function (grid) { let yLen = grid.length; if (!yLen) return 0; let xLen = grid[0].length; diff --git "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" index 55ce898..2a46b59 100644 --- "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" +++ "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" @@ -4,7 +4,7 @@ * @param {number[][]} grid * @return {number} */ -var maxAreaOfIsland = function (grid) { +let maxAreaOfIsland = function (grid) { let yLen = grid.length; if (!yLen) return grid; let xLen = grid[0].length; diff --git "a/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" index 4a1e385..9665db3 100644 --- "a/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" +++ "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\345\276\252\347\216\257\347\211\210.js" @@ -33,7 +33,7 @@ function genKey(i, j) { return `${i}-${j}`; } -var solve = function (board) { +let solve = function (board) { const notXMap = {}; for (let i = 0; i < board.length; i++) { let row = board[i]; diff --git "a/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" index f07ae19..686b90a 100644 --- "a/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" +++ "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237-\346\255\243\350\247\243\347\211\210.js" @@ -1,18 +1,18 @@ // https://github.com/sl1673495/daily-plan/issues/16 -var solve = function (board) { +let solve = function (board) { if (board.length == 0) return null; - for (var y = 0; y < board.length; y++) { - for (var x = 0; x < board[0].length; x++) { + for (let y = 0; y < board.length; y++) { + for (let x = 0; x < board[0].length; x++) { if (board[y][x] == "O" && (y == 0 || y == board.length - 1 || x == 0 || x == board[0].length - 1)) { dfs(board, y, x); } } } - for (var y = 0; y < board.length; y++) { - for (var x = 0; x < board[0].length; x++) { + for (let y = 0; y < board.length; y++) { + for (let x = 0; x < board[0].length; x++) { if (board[y][x] == "W") { board[y][x] = "O"; } else { diff --git "a/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" index a425546..716321b 100644 --- "a/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" +++ "b/DFS\351\227\256\351\242\230/\350\242\253\345\233\264\347\273\225\347\232\204\345\214\272\345\237\237.js" @@ -33,7 +33,7 @@ function genKey(i, j) { return `${i}-${j}`; } -var solve = function (board) { +let solve = function (board) { const notXMap = {}; for (let i = 0; i < board.length; i++) { let row = board[i]; diff --git a/sum-four-divisors.js b/sum-four-divisors.js index 5c4b056..aeaa8d5 100644 --- a/sum-four-divisors.js +++ b/sum-four-divisors.js @@ -2,10 +2,10 @@ * @param {number[]} nums * @return {number} */ -var sumFourDivisors = function(nums) { - var sum = 0 - for (var i = 0; i < nums.length; i++) { - var divisorSet = findDivisor(nums[i]) +let sumFourDivisors = function(nums) { + let sum = 0 + for (let i = 0; i < nums.length; i++) { + let divisorSet = findDivisor(nums[i]) if (divisorSet.size === 4) { divisorSet.forEach(num => { sum += num @@ -17,10 +17,10 @@ var sumFourDivisors = function(nums) { function findDivisor(num) { // 超过这个边界就不用继续求值了 - var max = Math.floor(Math.sqrt(num)) - var set = new Set() - for (var i = 1; i <= max; i ++) { - var result = num / i + let max = Math.floor(Math.sqrt(num)) + let set = new Set() + for (let i = 1; i <= max; i ++) { + let result = num / i // 除数是整数 if (result % 1 === 0) { set.add(result) diff --git "a/\344\270\211\346\225\260\344\271\213\345\222\214.js" "b/\344\270\211\346\225\260\344\271\213\345\222\214.js" index d2514d9..f44621b 100644 --- "a/\344\270\211\346\225\260\344\271\213\345\222\214.js" +++ "b/\344\270\211\346\225\260\344\271\213\345\222\214.js" @@ -2,9 +2,9 @@ * @param {number[]} nums * @return {number[][]} */ -var twoSum = function(nums, target) { +let twoSum = function(nums, target) { let map = new Map() - var results = [] + let results = [] for (let i = 0; i < nums.length; i++) { let num = nums[i] let result = map.get(target - num) @@ -16,11 +16,11 @@ var twoSum = function(nums, target) { return results } -var threeSum = function(nums) { +let threeSum = function(nums) { nums.sort((a, b) => a - b) - var set = new Set() - var results = [] - for (var i = 0; i < nums.length - 2; i++) { + let set = new Set() + let results = [] + for (let i = 0; i < nums.length - 2; i++) { let find = twoSum(nums.slice(i + 1), -nums[i]) if (find) { find.forEach((arr) => { diff --git "a/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" "b/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" index e6c0092..41339aa 100644 --- "a/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" +++ "b/\345\211\215K\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.js" @@ -17,20 +17,20 @@ 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。 */ -var topKFrequent = function(nums, k) { - var map = new Map() - for (var i = 0; i < nums.length; i++) { - var num = nums[i] - var count = map.get(num) +let topKFrequent = function(nums, k) { + let map = new Map() + for (let i = 0; i < nums.length; i++) { + let num = nums[i] + let count = map.get(num) if (!count) { map.set(num, 1) } else { map.set(num, count + 1) } } - var sorted = Array.from(map.entries()).sort((a, b) => b[1] - a[1]) - var result = [] - for (var i = 0; i < k; i++) { + let sorted = Array.from(map.entries()).sort((a, b) => b[1] - a[1]) + let result = [] + for (let i = 0; i < k; i++) { result.push(sorted[i][0]) } return result diff --git "a/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" "b/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" index da50d4a..138ee18 100644 --- "a/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" +++ "b/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" @@ -2,10 +2,10 @@ * @param {TreeNode} root * @return {boolean} */ -var isUnivalTree = function(root) { - var values = [] +let isUnivalTree = function(root) { + let values = [] dfs(root, values) - var first = values[0] + let first = values[0] return values.every(val => val === first) }; @@ -18,9 +18,9 @@ function dfs(node, values) { -var TreeNode = require('./二叉树.js') +let TreeNode = require('./二叉树.js') -var treeNode = new TreeNode(1) +let treeNode = new TreeNode(1) treeNode.left = new TreeNode(1) treeNode.right = new TreeNode(1) diff --git "a/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" "b/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" new file mode 100644 index 0000000..bb6ed6f --- /dev/null +++ "b/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @return {number} + */ +let removeDuplicates = function (nums) { + // 快指针 + let i = 0; + // 慢指针 + let j = 0; + + while (i < nums.length) { + let fast = nums[i]; + let slot = nums[j]; + + // 快慢不相等,说明找到了一个新的值 + // 把慢指针的位置更新,并且赋值成新的值,继续等待下一个新值。 + if (fast !== slot) { + j++; + nums[j] = num; + } + i++; + } + + console.log(nums); + + return j + 1; +}; + +console.log(removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])); diff --git "a/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" "b/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" index 404be99..7ee6b94 100644 --- "a/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" +++ "b/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" @@ -47,7 +47,7 @@ const merge = function(nums1, m, nums2, n) { } } -var a = [1, 2, 10, 0, 0, 0] +let a = [1, 2, 10, 0, 0, 0] merge(a, 3, [2, 5, 6, 7], 4) console.log(a) @@ -116,7 +116,7 @@ const merge2 = function(nums1, m, nums2, n) { } }; -var a2= [1, 2, 0, 0, 0, 0] +let a2= [1, 2, 0, 0, 0, 0] merge(a2, 2, [2, 5, 6], 4) console.log(a2) \ No newline at end of file diff --git "a/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" "b/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" index f6f7020..153df9a 100644 --- "a/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" +++ "b/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\351\223\276\350\241\250.js" @@ -3,11 +3,11 @@ function ListNode(val) { this.next = null } -var node1 = new ListNode(1) +let node1 = new ListNode(1) node1.next = new ListNode(2) node1.next.next = new ListNode(4) -var node2 = new ListNode(1) +let node2 = new ListNode(1) node2.next = new ListNode(3) node2.next.next = new ListNode(4) /** @@ -15,17 +15,17 @@ node2.next.next = new ListNode(4) * @param {ListNode} l2 * @return {ListNode} */ -var mergeTwoLists = function(l1, l2) { - var arr = [] +let mergeTwoLists = function(l1, l2) { + let arr = [] if (!l1 && !l2) { return null } while (l1 || l2) { - var runL1 = () => { + let runL1 = () => { arr.push(l1.val) l1 = l1.next } - var runL2 = () => { + let runL2 = () => { arr.push(l2.val) l2 = l2.next } diff --git "a/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" "b/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" index 6692cc0..9014e11 100644 --- "a/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" +++ "b/\345\255\227\347\254\246\344\270\262\345\255\227\347\254\246\346\234\200\347\237\255\350\267\257\345\276\204.js" @@ -17,8 +17,8 @@ S 和 C 中的所有字母均为小写字母。 * @param {character} C * @return {number[]} */ -var shortestToChar = function(S, C) { - var sl = S.length +let shortestToChar = function(S, C) { + let sl = S.length if (sl === 0) { return [] @@ -26,26 +26,26 @@ var shortestToChar = function(S, C) { if (sl.length > 10000) { S = S.substr(0, 10000) } - var pos = [] - var res = [] + let pos = [] + let res = [] // 先走一次循环 存储每个C值的位置 便于比对 - for (var i = 0; i < sl; i++) { + for (let i = 0; i < sl; i++) { if (S[i] === C) { pos.push(i) } } - for (var i = 0; i < sl; i++) { + for (let i = 0; i < sl; i++) { // 如果这个字符就是C 距离就是0 - var char = S[i] + let char = S[i] if (char === C) { res.push(0) } else { // 定一个最短距离变量 - var shortest = sl + let shortest = sl // 循环C位置的数组 找出距离最短的 注意绝对值 - for (var j = 0; j < pos.length; j++) { - var diff = pos[j] - i + for (let j = 0; j < pos.length; j++) { + let diff = pos[j] - i diff = diff > 0 ? diff : Math.abs(diff) if (diff < shortest) { shortest = diff diff --git "a/\345\275\222\345\271\266\346\216\222\345\272\217.js" "b/\345\275\222\345\271\266\346\216\222\345\272\217.js" index 8442c2d..d474b72 100644 --- "a/\345\275\222\345\271\266\346\216\222\345\272\217.js" +++ "b/\345\275\222\345\271\266\346\216\222\345\272\217.js" @@ -1,21 +1,21 @@ function mergeSort(arr) { - var l = arr.length; + let l = arr.length; if (l === 1) return arr; - var m = Math.round(l / 2); - var left = arr.slice(0, m); - var right = arr.slice(m); + let m = Math.round(l / 2); + let left = arr.slice(0, m); + let right = arr.slice(m); return merge(mergeSort(left), mergeSort(right)); } function merge(arr1, arr2) { - var l1 = arr1.length; - var l2 = arr2.length; - var i1 = 0; - var i2 = 0; - var r = []; + let l1 = arr1.length; + let l2 = arr2.length; + let i1 = 0; + let i2 = 0; + let r = []; while (i1 < l1 && i2 < l2) { - var item1 = arr1[i1]; - var item2 = arr2[i2]; + let item1 = arr1[i1]; + let item2 = arr2[i2]; if (item1 < item2) { r.push(item1); i1++; diff --git "a/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" "b/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" index 276679d..c2c9594 100644 --- "a/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" +++ "b/\346\211\276\345\207\272\345\255\220\345\255\227\347\254\246\344\270\262\347\254\254\344\270\200\346\254\241\345\207\272\347\216\260\347\232\204\344\275\215\347\275\256-28.js" @@ -3,13 +3,13 @@ * @param {string} needle * @return {number} */ -var strStr = function(haystack, needle) { +let strStr = function(haystack, needle) { if (needle === '') return 0 - var start - for (var i = 0; i < haystack.length - needle.length + 1; i++) { - for (var j = 0; j < needle.length; j++) { - var hIndex = i + j + let start + for (let i = 0; i < haystack.length - needle.length + 1; i++) { + for (let j = 0; j < needle.length; j++) { + let hIndex = i + j if (hIndex > haystack.length) { return -1 } diff --git "a/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" "b/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" index 1a99ef5..eeb11e8 100644 --- "a/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" +++ "b/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" @@ -23,22 +23,22 @@ 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 */ -var lengthOfLongestSubstring = function(str) { +let lengthOfLongestSubstring = function(str) { if (str.length === 1) { return 1 } // 滑动窗口 - var windows = [0, 0] - var max = 0 + let windows = [0, 0] + let max = 0 - for (var i = 0; i < str.length; i++) { - var char = str[i] + for (let i = 0; i < str.length; i++) { + let char = str[i] // 当前窗口中的文字 - var windowsStr = str.substring(windows[0], windows[1]) + let windowsStr = str.substring(windows[0], windows[1]) // 窗口中如果已经出现了这个文字 就需要把窗口左侧移动到「上一次出现这个文字」的右边位置 - var windowsCharIdx = windowsStr.indexOf(char) + let windowsCharIdx = windowsStr.indexOf(char) if (windowsCharIdx !== -1) { // 注意要加上窗口左侧已经移动的距离 windows[0] += windowsCharIdx + 1 @@ -47,7 +47,7 @@ var lengthOfLongestSubstring = function(str) { // 右边界始终后移 windows[1]++ - var windowsLength = windows[1] - windows[0] + let windowsLength = windows[1] - windows[0] if (windowsLength > max) { max = windowsLength } diff --git "a/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" "b/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" index 3cd3d29..5554471 100644 --- "a/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" +++ "b/\346\234\200\351\225\277\345\205\254\345\205\261\345\211\215\347\274\200.js" @@ -2,11 +2,11 @@ * @param {string[]} strs * @return {string} */ -var longestCommonPrefix = function (strs) { - var point = 0 - var common = "" - var shortestStr - var getShortestStr = false +let longestCommonPrefix = function (strs) { + let point = 0 + let common = "" + let shortestStr + let getShortestStr = false if (strs.length === 1) { return strs[0] @@ -16,8 +16,8 @@ var longestCommonPrefix = function (strs) { } while (1) { - for (var i = 0; i < strs.length; i++) { - var str = strs[i] + for (let i = 0; i < strs.length; i++) { + let str = strs[i] if (i > 0 && str[point] !== strs[i - 1][point]) { return common } diff --git "a/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" "b/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" index 35bff36..555d2e4 100644 --- "a/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" +++ "b/\346\234\211\345\272\217\346\225\260\347\273\204\346\261\202\344\272\244\351\233\206.js" @@ -1,33 +1,33 @@ -var a = [] +let a = [] for (let index = 0; index < 500; index++) { a.push(i) } -for (var i = 16; i < 10000; i++) { +for (let i = 16; i < 10000; i++) { a.push(i) } -var b = [] +let b = [] for (let index = 0; index < 500; index++) { b.push(i) } -for (var i = 10001; i < 50000; i++) { +for (let i = 10001; i < 50000; i++) { b.push(i) } function mapIntersection(arr1, arr2) { console.time() - var map = new Map() - for (var i = 0; i < arr1.length; i++) { + let map = new Map() + for (let i = 0; i < arr1.length; i++) { map.set(arr1[i], true) } - var result = [] - for (var i = 0; i < arr2.length; i++) { - var val = arr2[i] + let result = [] + for (let i = 0; i < arr2.length; i++) { + let val = arr2[i] if (map.get(val)) { result.push(val) } diff --git "a/\346\240\274\351\233\267\347\274\226\347\240\201.js" "b/\346\240\274\351\233\267\347\274\226\347\240\201.js" index e158ac2..bbc7aab 100644 --- "a/\346\240\274\351\233\267\347\274\226\347\240\201.js" +++ "b/\346\240\274\351\233\267\347\274\226\347\240\201.js" @@ -2,7 +2,7 @@ * @param {number} n * @return {number[]} */ -var grayCode = function(n) { +let grayCode = function(n) { // 用来算输入为n的格雷编码序列 let make = n => { if (n === 1) { diff --git "a/\347\247\215\350\212\261\351\227\256\351\242\230.js" "b/\347\247\215\350\212\261\351\227\256\351\242\230.js" index 7938777..f8ed412 100644 --- "a/\347\247\215\350\212\261\351\227\256\351\242\230.js" +++ "b/\347\247\215\350\212\261\351\227\256\351\242\230.js" @@ -23,12 +23,12 @@ * @param {number} n * @return {boolean} */ -var canPlaceFlowers = function(flowerbed, n) { +let canPlaceFlowers = function(flowerbed, n) { // empty代表连续命中0的次数 命中3次就说明要回退一个种花 - var empty = 1 - var count = 0 - for (var i = 0; i < flowerbed.length; i++) { - var has = flowerbed[i] + let empty = 1 + let count = 0 + for (let i = 0; i < flowerbed.length; i++) { + let has = flowerbed[i] if (!has) { empty++ // 连续三次空白 diff --git "a/\347\247\273\345\212\250\351\233\266-283.js" "b/\347\247\273\345\212\250\351\233\266-283.js" index 3aa4fae..deae007 100644 --- "a/\347\247\273\345\212\250\351\233\266-283.js" +++ "b/\347\247\273\345\212\250\351\233\266-283.js" @@ -44,11 +44,11 @@ * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ -var moveZeroes = function(nums) { - var j = 0 - for (var i = 0; i < nums.length; i++) { +let moveZeroes = function(nums) { + let j = 0 + for (let i = 0; i < nums.length; i++) { if (nums[i] !== 0) { - var temp = nums[j] + let temp = nums[j] nums[j] = nums[i] nums[i] = temp j++ diff --git "a/\347\247\273\351\231\244\345\205\203\347\264\240.js" "b/\347\247\273\351\231\244\345\205\203\347\264\240.js" index c03f041..2c89cec 100644 --- "a/\347\247\273\351\231\244\345\205\203\347\264\240.js" +++ "b/\347\247\273\351\231\244\345\205\203\347\264\240.js" @@ -29,7 +29,7 @@ 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 */ -var removeElement = function (nums, val) { +let removeElement = function (nums, val) { let i = 0; let j = 0; diff --git "a/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" "b/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" index e7ec87f..6a82ea5 100644 --- "a/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" +++ "b/\347\277\273\350\275\254\344\272\214\345\217\211\346\240\221.js" @@ -27,8 +27,8 @@ * @param {*} root * 遍历法,先循环找出所有需要翻转的treeNode,然后遍历翻转即可 */ -var invertTree = function(root) { - var queue = []; +let invertTree = function(root) { + let queue = []; function traverse(tree) { if (!tree) return; queue.push(tree); @@ -38,8 +38,8 @@ var invertTree = function(root) { traverse(root); while (queue.length) { - var node = queue.pop(); - var temp = node.right; + let node = queue.pop(); + let temp = node.right; node.right = node.left; node.left = temp; } diff --git "a/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" "b/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" index 1e19f23..8a4bd17 100644 --- "a/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" +++ "b/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" @@ -2,9 +2,9 @@ * @param {string} s * @return {boolean} */ -var validPalindrome = function(s) { - var i = 0; - var j = s.length - 1 +let validPalindrome = function(s) { + let i = 0; + let j = s.length - 1 // 两个指针往中间缩进 while (i < j && s[i] === s[j]) { diff --git "a/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" "b/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" index 2da1b03..bea2d21 100644 --- "a/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" +++ "b/\351\252\214\350\257\201\345\255\220\345\272\217\345\210\227.js" @@ -10,12 +10,12 @@ s = "abc", t = "ahbgdc" 返回 true. */ -// var isSubsequence = function(s, t) { -// var lastIndex = -1 -// var sliced = t -// var slicedCount = 0 -// for (var i = 0; i < s.length; i++) { -// var finded = sliced.indexOf(s[i]) +// let isSubsequence = function(s, t) { +// let lastIndex = -1 +// let sliced = t +// let slicedCount = 0 +// for (let i = 0; i < s.length; i++) { +// let finded = sliced.indexOf(s[i]) // if (finded + slicedCount > lastIndex) { // lastIndex = finded + slicedCount // sliced = sliced.substr(finded + 1) @@ -29,7 +29,7 @@ s = "abc", t = "ahbgdc" // } // 这种是速度最快的 利用indexOf的第二个参数做起点 -var isSubsequence = function(s, t) { +let isSubsequence = function(s, t) { let start=0 for(let i=0; i < s.length; i++) { let index = t.indexOf(s[i], start) @@ -39,6 +39,6 @@ var isSubsequence = function(s, t) { return true }; -var a = "leeeeetcode" -var b = 'leeeeeeeeeeccccctcccoddddeeee' +let a = "leeeeetcode" +let b = 'leeeeeeeeeeccccctcccoddddeeee' console.log(isSubsequence(a, b)) \ No newline at end of file diff --git "a/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" "b/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" index 36d75b6..1cc93a8 100644 --- "a/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" +++ "b/\351\252\214\350\257\201\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221.js" @@ -1,9 +1,9 @@ -var isValidBST = function(root) { +let isValidBST = function(root) { function helper(node, lower, upper) { // 遇到空节点 直接返回true if (!node) return true - var val = node.val + let val = node.val // 如果当前的值比下边界还小 就失败 if (lower !== null && val <= lower) return false // 如果当前的值比上边界还大 就失败 From 603e27fed78b2284d5986b9d455fee7d52484b03 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 3 May 2020 20:22:38 +0800 Subject: [PATCH 030/145] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 038ff2a..f3f639b 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,4 @@ ## 思路 -思路会记录在本仓库的 Issues 中,按照 label 进行分类。 +思路会记录在本仓库的 Issues 中,按照 label 进行分类。比如想查看 「DFS」 分类下的问题,那么选择标签进行筛选即可。 From 8af464adbbc2d74c3ab8dfe3bb18349723098d9d Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 4 May 2020 00:18:29 +0800 Subject: [PATCH 031/145] =?UTF-8?q?feat:=20=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- report.20200503.211357.57008.0.001.json | 453 ++++++++++++++++++ report.20200503.211524.56860.0.001.json | 453 ++++++++++++++++++ ...45\256\266\345\212\253\350\210\215-198.js" | 60 +++ ...256\266\345\212\253\350\210\215III-337.js" | 98 ++++ ...45\245\221\346\225\260\345\210\227-509.js" | 2 +- ...74\344\272\214\345\217\211\346\240\221.js" | 2 +- .../\344\272\214\345\217\211\346\240\221.js" | 0 7 files changed, 1066 insertions(+), 2 deletions(-) create mode 100644 report.20200503.211357.57008.0.001.json create mode 100644 report.20200503.211524.56860.0.001.json create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" rename "\344\272\214\345\217\211\346\240\221.js" => "\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" (100%) diff --git a/report.20200503.211357.57008.0.001.json b/report.20200503.211357.57008.0.001.json new file mode 100644 index 0000000..1fde733 --- /dev/null +++ b/report.20200503.211357.57008.0.001.json @@ -0,0 +1,453 @@ + +{ + "header": { + "reportVersion": 1, + "event": "Allocation failed - JavaScript heap out of memory", + "trigger": "FatalError", + "filename": "report.20200503.211357.57008.0.001.json", + "dumpEventTime": "2020-05-03T21:13:57Z", + "dumpEventTimeStamp": "1588511637443", + "processId": 57008, + "cwd": "c:\\codes\\leetcode-javascript", + "commandLine": [ + "node", + "c:\\codes\\leetcode-javascript\\动态规划\\打家劫舍III-337.js" + ], + "nodejsVersion": "v12.13.1", + "wordSize": 64, + "arch": "x64", + "platform": "win32", + "componentVersions": { + "node": "12.13.1", + "v8": "7.7.299.13-node.16", + "uv": "1.33.1", + "zlib": "1.2.11", + "brotli": "1.0.7", + "ares": "1.15.0", + "modules": "72", + "nghttp2": "1.39.2", + "napi": "5", + "llhttp": "1.1.4", + "http_parser": "2.8.0", + "openssl": "1.1.1d", + "cldr": "35.1", + "icu": "64.2", + "tz": "2019c", + "unicode": "12.1" + }, + "release": { + "name": "node", + "lts": "Erbium", + "headersUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1-headers.tar.gz", + "sourceUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1.tar.gz", + "libUrl": "https://nodejs.org/download/release/v12.13.1/win-x64/node.lib" + }, + "osName": "Windows_NT", + "osRelease": "10.0.18362", + "osVersion": "Windows 10 Home China", + "osMachine": "x86_64", + "cpus": [ + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3279531, + "nice": 0, + "sys": 2751609, + "idle": 140155812, + "irq": 603109 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1231875, + "nice": 0, + "sys": 818218, + "idle": 144136328, + "irq": 66671 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 5152671, + "nice": 0, + "sys": 2923187, + "idle": 138110562, + "irq": 71500 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 2180406, + "nice": 0, + "sys": 959343, + "idle": 143046671, + "irq": 22125 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3605000, + "nice": 0, + "sys": 2255015, + "idle": 140326406, + "irq": 55812 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1492312, + "nice": 0, + "sys": 883390, + "idle": 143810718, + "irq": 22812 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3256843, + "nice": 0, + "sys": 1774250, + "idle": 141155312, + "irq": 50312 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 2527296, + "nice": 0, + "sys": 1056890, + "idle": 142602218, + "irq": 35937 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3385750, + "nice": 0, + "sys": 2304171, + "idle": 140496484, + "irq": 64593 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1167796, + "nice": 0, + "sys": 931109, + "idle": 144087500, + "irq": 14453 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3003156, + "nice": 0, + "sys": 1937562, + "idle": 141245687, + "irq": 55984 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3453328, + "nice": 0, + "sys": 1274593, + "idle": 141458484, + "irq": 24015 + } + ], + "networkInterfaces": [ + { + "name": "Loopback Pseudo-Interface 1", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "::1", + "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", + "family": "IPv6", + "scopeid": 0 + }, + { + "name": "Loopback Pseudo-Interface 1", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "127.0.0.1", + "netmask": "255.0.0.0", + "family": "IPv4" + }, + { + "name": "WLAN", + "internal": false, + "mac": "50:e0:85:2f:fa:3a", + "address": "172.20.10.4", + "netmask": "255.255.255.240", + "family": "IPv4" + } + ], + "host": "LAPTOP-FMHNK1K6" + }, + "javascriptStack": { + "message": "No stack.", + "stack": [ + "Unavailable." + ] + }, + "nativeStack": [ + { + "pc": "0x00007ff6a40f1729", + "symbol": "std::basic_ostream >::operator<<+10873" + }, + { + "pc": "0x00007ff6a40f5b4c", + "symbol": "std::basic_ostream >::operator<<+28316" + }, + { + "pc": "0x00007ff6a40f4b08", + "symbol": "std::basic_ostream >::operator<<+24152" + }, + { + "pc": "0x00007ff6a41e369b", + "symbol": "v8::base::CPU::has_sse+37723" + }, + { + "pc": "0x00007ff6a49e82de", + "symbol": "v8::Isolate::ReportExternalAllocationLimitReached+94" + }, + { + "pc": "0x00007ff6a49d0321", + "symbol": "v8::SharedArrayBuffer::Externalize+833" + }, + { + "pc": "0x00007ff6a489dbec", + "symbol": "v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436" + }, + { + "pc": "0x00007ff6a48a8f90", + "symbol": "v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312" + }, + { + "pc": "0x00007ff6a48a5ac4", + "symbol": "v8::internal::Heap::PageFlagsAreConsistent+3204" + }, + { + "pc": "0x00007ff6a489b353", + "symbol": "v8::internal::Heap::CollectGarbage+1283" + }, + { + "pc": "0x00007ff6a4899b24", + "symbol": "v8::internal::Heap::AddRetainedMap+2356" + }, + { + "pc": "0x00007ff6a48b3c2e", + "symbol": "v8::internal::Factory::AllocateRawFixedArray+94" + }, + { + "pc": "0x00007ff6a48bafc4", + "symbol": "v8::internal::Factory::NewFixedArrayWithFiller+52" + }, + { + "pc": "0x00007ff6a48baf81", + "symbol": "v8::internal::Factory::NewUninitializedFixedArray+65" + }, + { + "pc": "0x00007ff6a479bdaf", + "symbol": "v8::Object::GetIsolate+6767" + }, + { + "pc": "0x00007ff6a464f13a", + "symbol": "v8::internal::OrderedHashMap::ValueAt+61274" + }, + { + "pc": "0x00007ff6a4e1404d", + "symbol": "v8::internal::SetupIsolateDelegate::SetupHeap+567949" + }, + { + "pc": "0x000002018f08794d", + "symbol": "" + } + ], + "javascriptHeap": { + "totalMemory": 2193383424, + "totalCommittedMemory": 2193383424, + "usedMemory": 2081107136, + "availableMemory": 42146160, + "memoryLimit": 2197815296, + "heapSpaces": { + "read_only_space": { + "memorySize": 262144, + "committedMemory": 262144, + "capacity": 261872, + "used": 32296, + "available": 229576 + }, + "new_space": { + "memorySize": 33554432, + "committedMemory": 33554432, + "capacity": 16759808, + "used": 32864, + "available": 16726944 + }, + "old_space": { + "memorySize": 599371776, + "committedMemory": 599371776, + "capacity": 549298736, + "used": 545300776, + "available": 3997960 + }, + "code_space": { + "memorySize": 425984, + "committedMemory": 425984, + "capacity": 160896, + "used": 160896, + "available": 0 + }, + "map_space": { + "memorySize": 266240, + "committedMemory": 266240, + "capacity": 178080, + "used": 178080, + "available": 0 + }, + "large_object_space": { + "memorySize": 1559453696, + "committedMemory": 1559453696, + "capacity": 1535398672, + "used": 1535398672, + "available": 0 + }, + "code_large_object_space": { + "memorySize": 49152, + "committedMemory": 49152, + "capacity": 3552, + "used": 3552, + "available": 0 + }, + "new_large_object_space": { + "memorySize": 0, + "committedMemory": 0, + "capacity": 16759808, + "used": 0, + "available": 16759808 + } + } + }, + "resourceUsage": { + "userCpuSeconds": 8.312, + "kernelCpuSeconds": 1.625, + "cpuConsumptionPercent": 141.957, + "maxRss": 2224611328, + "pageFaults": { + "IORequired": 722070, + "IONotRequired": 0 + }, + "fsActivity": { + "reads": 2, + "writes": 110 + } + }, + "libuv": [ + ], + "environmentVariables": { + "=C:": "c:\\codes\\leetcode-javascript", + "ALLUSERSPROFILE": "C:\\ProgramData", + "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", + "APPDATA": "C:\\Users\\94958\\AppData\\Roaming", + "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", + "CommonProgramFiles": "C:\\Program Files\\Common Files", + "CommonProgramFiles(x86)": "C:\\Program Files (x86)\\Common Files", + "CommonProgramW6432": "C:\\Program Files\\Common Files", + "COMPUTERNAME": "LAPTOP-FMHNK1K6", + "ComSpec": "C:\\Windows\\system32\\cmd.exe", + "DriverData": "C:\\Windows\\System32\\Drivers\\DriverData", + "ELECTRON_RUN_AS_NODE": "1", + "GameFirst": "C:\\Program Files (x86)\\ASUS\\GameFirst\\", + "GameFirstUserPath": "C:\\Users\\94958\\AppData\\Local\\ASUS\\GameFirst", + "HOMEDRIVE": "C:", + "HOMEPATH": "\\Users\\94958", + "LOCALAPPDATA": "C:\\Users\\94958\\AppData\\Local", + "LOGONSERVER": "\\\\LAPTOP-FMHNK1K6", + "NUMBER_OF_PROCESSORS": "12", + "OneDrive": "C:\\Users\\94958\\OneDrive", + "OneDriveConsumer": "C:\\Users\\94958\\OneDrive", + "OS": "Windows_NT", + "Path": "C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\nodejs\\;C:\\Users\\94958\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\94958\\AppData\\Roaming\\npm;C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", + "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", + "PIPE_LOGGING": "true", + "PROCESSOR_ARCHITECTURE": "AMD64", + "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 10, GenuineIntel", + "PROCESSOR_LEVEL": "6", + "PROCESSOR_REVISION": "9e0a", + "ProgramData": "C:\\ProgramData", + "ProgramFiles": "C:\\Program Files", + "ProgramFiles(x86)": "C:\\Program Files (x86)", + "ProgramW6432": "C:\\Program Files", + "PROMPT": "$P$G", + "PSModulePath": "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules", + "PUBLIC": "C:\\Users\\Public", + "SESSIONNAME": "Console", + "SystemDrive": "C:", + "SystemRoot": "C:\\Windows", + "TEMP": "C:\\Users\\94958\\AppData\\Local\\Temp", + "TMP": "C:\\Users\\94958\\AppData\\Local\\Temp", + "USERDOMAIN": "LAPTOP-FMHNK1K6", + "USERDOMAIN_ROAMINGPROFILE": "LAPTOP-FMHNK1K6", + "USERNAME": "94958", + "USERPROFILE": "C:\\Users\\94958", + "VERBOSE_LOGGING": "true", + "VSCODE_CWD": "C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code", + "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", + "VSCODE_IPC_HOOK": "\\\\.\\pipe\\b8cbe1eb44c59330f23b7126ffc1d83e-1.44.2-main-sock", + "VSCODE_IPC_HOOK_EXTHOST": "\\\\.\\pipe\\vscode-ipc-ca5d424d-f130-4cc5-b06e-3552ea0b7394-sock", + "VSCODE_LOGS": "C:\\Users\\94958\\AppData\\Roaming\\Code\\logs\\20200503T205835", + "VSCODE_LOG_STACK": "false", + "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_translationsConfigFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\tcf.json\",\"_cacheRoot\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\ff915844119ce9485abfe8aa9076ec76b5300ddd\",\"_corruptedFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\corrupted.info\",\"_languagePackSupport\":true}", + "VSCODE_NODE_CACHED_DATA_DIR": "C:\\Users\\94958\\AppData\\Roaming\\Code\\CachedData\\ff915844119ce9485abfe8aa9076ec76b5300ddd", + "VSCODE_PID": "56608", + "windir": "C:\\Windows" + }, + "sharedObjects": [ + "C:\\Program Files\\nodejs\\node.exe", + "C:\\Windows\\SYSTEM32\\ntdll.dll", + "C:\\Windows\\System32\\KERNEL32.DLL", + "C:\\Windows\\System32\\KERNELBASE.dll", + "C:\\Windows\\System32\\WS2_32.dll", + "C:\\Windows\\System32\\RPCRT4.dll", + "C:\\Windows\\System32\\ADVAPI32.dll", + "C:\\Windows\\SYSTEM32\\dbghelp.dll", + "C:\\Windows\\System32\\msvcrt.dll", + "C:\\Windows\\System32\\ucrtbase.dll", + "C:\\Windows\\System32\\sechost.dll", + "C:\\Windows\\System32\\USER32.dll", + "C:\\Windows\\System32\\win32u.dll", + "C:\\Windows\\System32\\GDI32.dll", + "C:\\Windows\\System32\\gdi32full.dll", + "C:\\Windows\\System32\\msvcp_win.dll", + "C:\\Windows\\System32\\PSAPI.DLL", + "C:\\Windows\\System32\\CRYPT32.dll", + "C:\\Windows\\System32\\MSASN1.dll", + "C:\\Windows\\SYSTEM32\\IPHLPAPI.DLL", + "C:\\Windows\\System32\\bcrypt.dll", + "C:\\Windows\\SYSTEM32\\USERENV.dll", + "C:\\Windows\\System32\\profapi.dll", + "C:\\Windows\\SYSTEM32\\WINMM.dll", + "C:\\Windows\\SYSTEM32\\winmmbase.dll", + "C:\\Windows\\System32\\cfgmgr32.dll", + "C:\\Windows\\System32\\bcryptPrimitives.dll", + "C:\\Windows\\System32\\IMM32.DLL", + "C:\\Windows\\System32\\powrprof.dll", + "C:\\Windows\\System32\\UMPDC.dll", + "C:\\Windows\\SYSTEM32\\CRYPTBASE.DLL", + "C:\\Windows\\system32\\uxtheme.dll", + "C:\\Windows\\System32\\combase.dll", + "C:\\Windows\\system32\\mswsock.dll", + "C:\\Windows\\System32\\kernel.appcore.dll", + "C:\\Windows\\System32\\NSI.dll", + "C:\\Windows\\SYSTEM32\\dhcpcsvc6.DLL", + "C:\\Windows\\SYSTEM32\\dhcpcsvc.DLL", + "C:\\Windows\\SYSTEM32\\DNSAPI.dll", + "C:\\Windows\\system32\\napinsp.dll", + "C:\\Windows\\system32\\pnrpnsp.dll", + "C:\\Windows\\System32\\winrnr.dll", + "C:\\Windows\\system32\\NLAapi.dll", + "C:\\Windows\\system32\\wshbth.dll" + ] +} \ No newline at end of file diff --git a/report.20200503.211524.56860.0.001.json b/report.20200503.211524.56860.0.001.json new file mode 100644 index 0000000..4131636 --- /dev/null +++ b/report.20200503.211524.56860.0.001.json @@ -0,0 +1,453 @@ + +{ + "header": { + "reportVersion": 1, + "event": "Allocation failed - JavaScript heap out of memory", + "trigger": "FatalError", + "filename": "report.20200503.211524.56860.0.001.json", + "dumpEventTime": "2020-05-03T21:15:24Z", + "dumpEventTimeStamp": "1588511724189", + "processId": 56860, + "cwd": "c:\\codes\\leetcode-javascript", + "commandLine": [ + "node", + "c:\\codes\\leetcode-javascript\\动态规划\\打家劫舍III-337.js" + ], + "nodejsVersion": "v12.13.1", + "wordSize": 64, + "arch": "x64", + "platform": "win32", + "componentVersions": { + "node": "12.13.1", + "v8": "7.7.299.13-node.16", + "uv": "1.33.1", + "zlib": "1.2.11", + "brotli": "1.0.7", + "ares": "1.15.0", + "modules": "72", + "nghttp2": "1.39.2", + "napi": "5", + "llhttp": "1.1.4", + "http_parser": "2.8.0", + "openssl": "1.1.1d", + "cldr": "35.1", + "icu": "64.2", + "tz": "2019c", + "unicode": "12.1" + }, + "release": { + "name": "node", + "lts": "Erbium", + "headersUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1-headers.tar.gz", + "sourceUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1.tar.gz", + "libUrl": "https://nodejs.org/download/release/v12.13.1/win-x64/node.lib" + }, + "osName": "Windows_NT", + "osRelease": "10.0.18362", + "osVersion": "Windows 10 Home China", + "osMachine": "x86_64", + "cpus": [ + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3296453, + "nice": 0, + "sys": 2762812, + "idle": 140214437, + "irq": 605781 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1244515, + "nice": 0, + "sys": 821750, + "idle": 144206906, + "irq": 67578 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 5176453, + "nice": 0, + "sys": 2930281, + "idle": 138166437, + "irq": 71828 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 2193328, + "nice": 0, + "sys": 963750, + "idle": 143116093, + "irq": 22359 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3621390, + "nice": 0, + "sys": 2261281, + "idle": 140390500, + "irq": 56046 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1504703, + "nice": 0, + "sys": 886625, + "idle": 143881843, + "irq": 22859 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3272625, + "nice": 0, + "sys": 1780046, + "idle": 141220484, + "irq": 50531 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 2539703, + "nice": 0, + "sys": 1061375, + "idle": 142672078, + "irq": 36078 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3401375, + "nice": 0, + "sys": 2311562, + "idle": 140560218, + "irq": 64843 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1174734, + "nice": 0, + "sys": 933906, + "idle": 144164515, + "irq": 14515 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3018156, + "nice": 0, + "sys": 1943500, + "idle": 141311500, + "irq": 56203 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 3469390, + "nice": 0, + "sys": 1280562, + "idle": 141523203, + "irq": 24203 + } + ], + "networkInterfaces": [ + { + "name": "Loopback Pseudo-Interface 1", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "::1", + "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", + "family": "IPv6", + "scopeid": 0 + }, + { + "name": "Loopback Pseudo-Interface 1", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "127.0.0.1", + "netmask": "255.0.0.0", + "family": "IPv4" + }, + { + "name": "WLAN", + "internal": false, + "mac": "50:e0:85:2f:fa:3a", + "address": "172.20.10.4", + "netmask": "255.255.255.240", + "family": "IPv4" + } + ], + "host": "LAPTOP-FMHNK1K6" + }, + "javascriptStack": { + "message": "No stack.", + "stack": [ + "Unavailable." + ] + }, + "nativeStack": [ + { + "pc": "0x00007ff6a40f1729", + "symbol": "std::basic_ostream >::operator<<+10873" + }, + { + "pc": "0x00007ff6a40f5b4c", + "symbol": "std::basic_ostream >::operator<<+28316" + }, + { + "pc": "0x00007ff6a40f4b08", + "symbol": "std::basic_ostream >::operator<<+24152" + }, + { + "pc": "0x00007ff6a41e369b", + "symbol": "v8::base::CPU::has_sse+37723" + }, + { + "pc": "0x00007ff6a49e82de", + "symbol": "v8::Isolate::ReportExternalAllocationLimitReached+94" + }, + { + "pc": "0x00007ff6a49d0321", + "symbol": "v8::SharedArrayBuffer::Externalize+833" + }, + { + "pc": "0x00007ff6a489dbec", + "symbol": "v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436" + }, + { + "pc": "0x00007ff6a48a8f90", + "symbol": "v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312" + }, + { + "pc": "0x00007ff6a48a5ac4", + "symbol": "v8::internal::Heap::PageFlagsAreConsistent+3204" + }, + { + "pc": "0x00007ff6a489b353", + "symbol": "v8::internal::Heap::CollectGarbage+1283" + }, + { + "pc": "0x00007ff6a4899b24", + "symbol": "v8::internal::Heap::AddRetainedMap+2356" + }, + { + "pc": "0x00007ff6a48b3c2e", + "symbol": "v8::internal::Factory::AllocateRawFixedArray+94" + }, + { + "pc": "0x00007ff6a48bafc4", + "symbol": "v8::internal::Factory::NewFixedArrayWithFiller+52" + }, + { + "pc": "0x00007ff6a48baf81", + "symbol": "v8::internal::Factory::NewUninitializedFixedArray+65" + }, + { + "pc": "0x00007ff6a479bdaf", + "symbol": "v8::Object::GetIsolate+6767" + }, + { + "pc": "0x00007ff6a464f13a", + "symbol": "v8::internal::OrderedHashMap::ValueAt+61274" + }, + { + "pc": "0x00007ff6a4e1404d", + "symbol": "v8::internal::SetupIsolateDelegate::SetupHeap+567949" + }, + { + "pc": "0x0000005906d4794d", + "symbol": "" + } + ], + "javascriptHeap": { + "totalMemory": 2194755584, + "totalCommittedMemory": 2194755584, + "usedMemory": 2082495824, + "availableMemory": 39710424, + "memoryLimit": 2197815296, + "heapSpaces": { + "read_only_space": { + "memorySize": 262144, + "committedMemory": 262144, + "capacity": 261872, + "used": 32296, + "available": 229576 + }, + "new_space": { + "memorySize": 33554432, + "committedMemory": 33554432, + "capacity": 16759808, + "used": 65568, + "available": 16694240 + }, + "old_space": { + "memorySize": 599371776, + "committedMemory": 599371776, + "capacity": 548268088, + "used": 545301000, + "available": 2967088 + }, + "code_space": { + "memorySize": 425984, + "committedMemory": 425984, + "capacity": 160896, + "used": 160896, + "available": 0 + }, + "map_space": { + "memorySize": 266240, + "committedMemory": 266240, + "capacity": 178080, + "used": 178080, + "available": 0 + }, + "large_object_space": { + "memorySize": 1560825856, + "committedMemory": 1560825856, + "capacity": 1536754432, + "used": 1536754432, + "available": 0 + }, + "code_large_object_space": { + "memorySize": 49152, + "committedMemory": 49152, + "capacity": 3552, + "used": 3552, + "available": 0 + }, + "new_large_object_space": { + "memorySize": 0, + "committedMemory": 0, + "capacity": 16759808, + "used": 0, + "available": 16759808 + } + } + }, + "resourceUsage": { + "userCpuSeconds": 10.656, + "kernelCpuSeconds": 1.5, + "cpuConsumptionPercent": 151.95, + "maxRss": 2226262016, + "pageFaults": { + "IORequired": 722857, + "IONotRequired": 0 + }, + "fsActivity": { + "reads": 2, + "writes": 110 + } + }, + "libuv": [ + ], + "environmentVariables": { + "=C:": "c:\\codes\\leetcode-javascript", + "ALLUSERSPROFILE": "C:\\ProgramData", + "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", + "APPDATA": "C:\\Users\\94958\\AppData\\Roaming", + "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", + "CommonProgramFiles": "C:\\Program Files\\Common Files", + "CommonProgramFiles(x86)": "C:\\Program Files (x86)\\Common Files", + "CommonProgramW6432": "C:\\Program Files\\Common Files", + "COMPUTERNAME": "LAPTOP-FMHNK1K6", + "ComSpec": "C:\\Windows\\system32\\cmd.exe", + "DriverData": "C:\\Windows\\System32\\Drivers\\DriverData", + "ELECTRON_RUN_AS_NODE": "1", + "GameFirst": "C:\\Program Files (x86)\\ASUS\\GameFirst\\", + "GameFirstUserPath": "C:\\Users\\94958\\AppData\\Local\\ASUS\\GameFirst", + "HOMEDRIVE": "C:", + "HOMEPATH": "\\Users\\94958", + "LOCALAPPDATA": "C:\\Users\\94958\\AppData\\Local", + "LOGONSERVER": "\\\\LAPTOP-FMHNK1K6", + "NUMBER_OF_PROCESSORS": "12", + "OneDrive": "C:\\Users\\94958\\OneDrive", + "OneDriveConsumer": "C:\\Users\\94958\\OneDrive", + "OS": "Windows_NT", + "Path": "C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\nodejs\\;C:\\Users\\94958\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\94958\\AppData\\Roaming\\npm;C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", + "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", + "PIPE_LOGGING": "true", + "PROCESSOR_ARCHITECTURE": "AMD64", + "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 10, GenuineIntel", + "PROCESSOR_LEVEL": "6", + "PROCESSOR_REVISION": "9e0a", + "ProgramData": "C:\\ProgramData", + "ProgramFiles": "C:\\Program Files", + "ProgramFiles(x86)": "C:\\Program Files (x86)", + "ProgramW6432": "C:\\Program Files", + "PROMPT": "$P$G", + "PSModulePath": "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules", + "PUBLIC": "C:\\Users\\Public", + "SESSIONNAME": "Console", + "SystemDrive": "C:", + "SystemRoot": "C:\\Windows", + "TEMP": "C:\\Users\\94958\\AppData\\Local\\Temp", + "TMP": "C:\\Users\\94958\\AppData\\Local\\Temp", + "USERDOMAIN": "LAPTOP-FMHNK1K6", + "USERDOMAIN_ROAMINGPROFILE": "LAPTOP-FMHNK1K6", + "USERNAME": "94958", + "USERPROFILE": "C:\\Users\\94958", + "VERBOSE_LOGGING": "true", + "VSCODE_CWD": "C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code", + "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", + "VSCODE_IPC_HOOK": "\\\\.\\pipe\\b8cbe1eb44c59330f23b7126ffc1d83e-1.44.2-main-sock", + "VSCODE_IPC_HOOK_EXTHOST": "\\\\.\\pipe\\vscode-ipc-ca5d424d-f130-4cc5-b06e-3552ea0b7394-sock", + "VSCODE_LOGS": "C:\\Users\\94958\\AppData\\Roaming\\Code\\logs\\20200503T205835", + "VSCODE_LOG_STACK": "false", + "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_translationsConfigFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\tcf.json\",\"_cacheRoot\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\ff915844119ce9485abfe8aa9076ec76b5300ddd\",\"_corruptedFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\corrupted.info\",\"_languagePackSupport\":true}", + "VSCODE_NODE_CACHED_DATA_DIR": "C:\\Users\\94958\\AppData\\Roaming\\Code\\CachedData\\ff915844119ce9485abfe8aa9076ec76b5300ddd", + "VSCODE_PID": "56608", + "windir": "C:\\Windows" + }, + "sharedObjects": [ + "C:\\Program Files\\nodejs\\node.exe", + "C:\\Windows\\SYSTEM32\\ntdll.dll", + "C:\\Windows\\System32\\KERNEL32.DLL", + "C:\\Windows\\System32\\KERNELBASE.dll", + "C:\\Windows\\System32\\WS2_32.dll", + "C:\\Windows\\System32\\RPCRT4.dll", + "C:\\Windows\\SYSTEM32\\dbghelp.dll", + "C:\\Windows\\System32\\ADVAPI32.dll", + "C:\\Windows\\System32\\msvcrt.dll", + "C:\\Windows\\System32\\ucrtbase.dll", + "C:\\Windows\\System32\\sechost.dll", + "C:\\Windows\\System32\\USER32.dll", + "C:\\Windows\\System32\\win32u.dll", + "C:\\Windows\\System32\\GDI32.dll", + "C:\\Windows\\System32\\gdi32full.dll", + "C:\\Windows\\System32\\msvcp_win.dll", + "C:\\Windows\\System32\\PSAPI.DLL", + "C:\\Windows\\System32\\CRYPT32.dll", + "C:\\Windows\\System32\\MSASN1.dll", + "C:\\Windows\\System32\\bcrypt.dll", + "C:\\Windows\\SYSTEM32\\IPHLPAPI.DLL", + "C:\\Windows\\SYSTEM32\\USERENV.dll", + "C:\\Windows\\System32\\profapi.dll", + "C:\\Windows\\SYSTEM32\\WINMM.dll", + "C:\\Windows\\SYSTEM32\\winmmbase.dll", + "C:\\Windows\\System32\\cfgmgr32.dll", + "C:\\Windows\\System32\\bcryptPrimitives.dll", + "C:\\Windows\\System32\\IMM32.DLL", + "C:\\Windows\\System32\\powrprof.dll", + "C:\\Windows\\System32\\UMPDC.dll", + "C:\\Windows\\SYSTEM32\\CRYPTBASE.DLL", + "C:\\Windows\\system32\\uxtheme.dll", + "C:\\Windows\\System32\\combase.dll", + "C:\\Windows\\system32\\mswsock.dll", + "C:\\Windows\\System32\\kernel.appcore.dll", + "C:\\Windows\\System32\\NSI.dll", + "C:\\Windows\\SYSTEM32\\dhcpcsvc6.DLL", + "C:\\Windows\\SYSTEM32\\dhcpcsvc.DLL", + "C:\\Windows\\SYSTEM32\\DNSAPI.dll", + "C:\\Windows\\system32\\napinsp.dll", + "C:\\Windows\\system32\\pnrpnsp.dll", + "C:\\Windows\\System32\\winrnr.dll", + "C:\\Windows\\system32\\NLAapi.dll", + "C:\\Windows\\system32\\wshbth.dll" + ] +} \ No newline at end of file diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" new file mode 100644 index 0000000..e2233ee --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" @@ -0,0 +1,60 @@ +let assert = require("assert"); + +/** + * 打家劫舍 递归版 + * @param {number[]} nums + * @return {number} + */ +let robRecurision = function (nums) { + // 考虑从 index 开始到 nums - 1 为止 + // 打劫的最高价值 + let tryRob = function (nums, index) { + // 超出边界了 + if (index >= nums.length) { + return 0; + } + + let max = 0; + // 从 index,...n 分别选作起点开始抢劫 + // 求出各个值作为起点的最优解 + for (let i = index; i < nums.length; i++) { + let value = nums[i]; + + // 不能打劫邻舍 所以要 +2 + max = Math.max(max, value + tryRob(nums, i + 2)); + } + + return max; + }; + + return tryRob(nums, 0); +}; + +assert(robRecurision([1, 2, 3, 1]) === 4); +assert(robRecurision([2, 7, 9, 3, 1]) === 12); + +/** + * 打家劫舍 DP版 + * @param {number[]} nums + * @return {number} + */ +let rob = function (nums) { + if (!nums.length) { + return 0; + } + let dp = []; + + for (let i = nums.length - 1; i >= 0; i--) { + let max = 0; + for (let index = i; index < nums.length; index++) { + let value = nums[index]; + max = Math.max(max, value + (dp[index + 2] || 0)); + } + dp[i] = max; + } + + return dp[0]; +}; + +assert(rob([1, 2, 3, 1]) === 4); +assert(rob([2, 7, 9, 3, 1]) === 12); diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" new file mode 100644 index 0000000..c6bf067 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" @@ -0,0 +1,98 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +const TreeNode = require("../工具/二叉树"); + +/** + * 65 / 124 个通过测试用例 + */ + +/** + * @param {TreeNode} root + * @return {number} + */ +/** + * @param {TreeNode} root + * @return {number} + */ +let rob = function (root) { + if (!root) return 0; + + // 先求出树的层级 以及每层对应的所有节点 + const { maxLevel, levelNodesMap } = makeLevelNodesMap(root); + + let getLevelValue = (level) => { + const nodes = levelNodesMap.get(level); + if (!nodes) { + return 0; + } + let value = 0; + for (let i = 0; i < nodes.length; i++) { + value += nodes[i].val; + } + return value; + }; + + // 接下来开始进入打劫时间 + let dp = []; + for (let i = maxLevel; i >= 0; i--) { + let max = 0; + // 每次需要跳过一个层级,不能打劫相邻的房子。 + for (j = i; j <= maxLevel; j++) { + // 当前层级的价值加上跳一层后为起点的打劫的最大价值 + max = Math.max(max, getLevelValue(j) + (dp[j + 2] || 0)); + } + dp[i] = max; + } + + return dp[0]; +}; + +function makeLevelNodesMap(root) { + let queue = [root]; + // 每一层级的节点记录下来 + let levelNodesMap = new Map(); + let maxLevel = 0; + while (queue.length) { + // 缓存当前层级的节点长度 + let levelLen = queue.length; + let levelNodes = []; + for (let i = 0; i < levelLen; i++) { + let node = queue[i]; + levelNodes.push(node); + + // 这里是下一层级了 先放入队列中 + if (node.left) { + queue.push(node.left); + } + if (node.right) { + queue.push(node.right); + } + } + + // 本层处理完毕 移除出数组 + for (let i = 0; i < levelLen; i++) { + queue.shift(); + } + + levelNodesMap.set(maxLevel, levelNodes); + if (queue.length) { + maxLevel++; + } + } + + return { maxLevel, levelNodesMap }; +} + +// 这种情况是不对的 应该计算出7 +let tree1 = new TreeNode(2); +tree1.left = new TreeNode(1); +tree1.right = new TreeNode(3); +tree1.left.left = new TreeNode(null); +tree1.left.right = new TreeNode(4); + +console.log(rob(tree1)); diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" index af64b5d..626c8bd 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" @@ -40,4 +40,4 @@ let fib = function (N) { // }; // })(); -console.log(fib(10000)); +console.log(fib(100000)); diff --git "a/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" "b/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" index 138ee18..1c862a2 100644 --- "a/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" +++ "b/\345\215\225\345\200\274\344\272\214\345\217\211\346\240\221.js" @@ -18,7 +18,7 @@ function dfs(node, values) { -let TreeNode = require('./二叉树.js') +let TreeNode = require('./工具/二叉树.js') let treeNode = new TreeNode(1) diff --git "a/\344\272\214\345\217\211\346\240\221.js" "b/\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" similarity index 100% rename from "\344\272\214\345\217\211\346\240\221.js" rename to "\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" From 378a297aff0a81d9dcea2c51fba3400769a317fc Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Mon, 4 May 2020 11:00:02 +0800 Subject: [PATCH 032/145] =?UTF-8?q?feat:=20=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D|||?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...256\266\345\212\253\350\210\215III-337.js" | 79 ++++--------------- 1 file changed, 16 insertions(+), 63 deletions(-) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" index c6bf067..af183a9 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215III-337.js" @@ -19,74 +19,27 @@ const TreeNode = require("../工具/二叉树"); * @param {TreeNode} root * @return {number} */ +let memo = new WeakMap() let rob = function (root) { - if (!root) return 0; - - // 先求出树的层级 以及每层对应的所有节点 - const { maxLevel, levelNodesMap } = makeLevelNodesMap(root); - - let getLevelValue = (level) => { - const nodes = levelNodesMap.get(level); - if (!nodes) { - return 0; - } - let value = 0; - for (let i = 0; i < nodes.length; i++) { - value += nodes[i].val; - } - return value; - }; - - // 接下来开始进入打劫时间 - let dp = []; - for (let i = maxLevel; i >= 0; i--) { - let max = 0; - // 每次需要跳过一个层级,不能打劫相邻的房子。 - for (j = i; j <= maxLevel; j++) { - // 当前层级的价值加上跳一层后为起点的打劫的最大价值 - max = Math.max(max, getLevelValue(j) + (dp[j + 2] || 0)); - } - dp[i] = max; + if (!root) { + return 0; } - return dp[0]; -}; - -function makeLevelNodesMap(root) { - let queue = [root]; - // 每一层级的节点记录下来 - let levelNodesMap = new Map(); - let maxLevel = 0; - while (queue.length) { - // 缓存当前层级的节点长度 - let levelLen = queue.length; - let levelNodes = []; - for (let i = 0; i < levelLen; i++) { - let node = queue[i]; - levelNodes.push(node); - - // 这里是下一层级了 先放入队列中 - if (node.left) { - queue.push(node.left); - } - if (node.right) { - queue.push(node.right); - } - } - - // 本层处理完毕 移除出数组 - for (let i = 0; i < levelLen; i++) { - queue.shift(); - } - - levelNodesMap.set(maxLevel, levelNodes); - if (queue.length) { - maxLevel++; - } + let memorized = memo.get(root) + if (memorized) { + return memorized } - return { maxLevel, levelNodesMap }; -} + let notRob = rob(root.left) + rob(root.right); + let robNow = + (root.val || 0) + + (root.left ? rob(root.left.left) + rob(root.left.right) : 0) + + (root.right ? rob(root.right.left) + rob(root.right.right) : 0); + + let max = Math.max(notRob, robNow); + memo.set(root, max) + return max; +}; // 这种情况是不对的 应该计算出7 let tree1 = new TreeNode(2); From e4dbb955982d2878dee76a4b825251f7a02d5089 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Mon, 4 May 2020 21:06:02 +0800 Subject: [PATCH 033/145] =?UTF-8?q?feat:=20=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\222\261\345\205\221\346\215\242II-518.js" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" new file mode 100644 index 0000000..d2fcc5e --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" @@ -0,0 +1,22 @@ +/** + * @param {number} amount + * @param {number[]} coins + * @return {number} + */ +let change = function (amount, coins) { + let dp = new Array(amount + 1).fill(0); + + dp[0] = 1; + + for (let coin of coins) { + for (let i = 1; i <= amount; i++) { + if (i >= coin) { + dp[i] += dp[i - coin]; + } + } + } + + return dp[amount]; +}; + +console.log(change(5, [1, 2, 5])); From 49162f69332291c7dfe0d754df191ddd9d00c2d0 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 5 May 2020 02:49:57 +0800 Subject: [PATCH 034/145] =?UTF-8?q?feat:=2001=E8=83=8C=E5=8C=85-=E9=80=92?= =?UTF-8?q?=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5-\351\200\222\345\275\222\347\211\210.js" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" new file mode 100644 index 0000000..c57ef28 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" @@ -0,0 +1,33 @@ +/** + * + * @param {number[]} w 物品的重量集合 + * @param {number[]} v 物品的价值集合 + * @param {number} C 背包容量 + */ +function knapsack01(w, v, C) { + let n = w.length - 1; + + return bestValue(w, v, n, C); +} + +// 用 [0...index] 的物品 +// 填充容积为c的背包的最大价值 +function bestValue(w, v, index, c) { + if (index < 0 || c <= 0) return 0; + + let max = bestValue(w, v, index - 1, c); + + // 装背包之前需要先判断这个当前背包还可以容纳下这个物品 + if (c >= w[index]) { + max = Math.max( + // 不装进背包 + max, + // 装进背包 + v[index] + bestValue(w, v, index - 1, c - w[index]) + ); + } + + return max; +} + +console.log(knapsack01([1, 2, 3], [4, 5, 15], 3)); From 51fc39733b04314ad4e7bc40637320b3439a1fec Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 5 May 2020 12:08:22 +0800 Subject: [PATCH 035/145] =?UTF-8?q?feat:=20=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8D=E7=AC=AC=E4=BA=8C=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\256\266\345\212\253\350\210\215-198.js" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" index e2233ee..17c64e8 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\223\345\256\266\345\212\253\350\210\215-198.js" @@ -33,6 +33,39 @@ let robRecurision = function (nums) { assert(robRecurision([1, 2, 3, 1]) === 4); assert(robRecurision([2, 7, 9, 3, 1]) === 12); +/** + * 打家劫舍-递归版2 + * 把打劫细分为 + * + * 1. 打劫当前房子 那么下次就要从start + 2开始 + * 2. 打劫下一个房子 那么就直接从start + 1开始 + * + * 求这两者间的最大值 + * @param {*} nums + */ +let robRecurision2 = function (nums) { + let memo = []; + function tryRob(nums, start) { + let memorized = memo[start]; + if (memorized) return memorized; + + if (start > nums.length - 1) return 0; + + let robNow = nums[start] + tryRob(nums, start + 2); + let robNext = tryRob(nums, start + 1); + + let best = Math.max(robNext, robNow); + memo[start] = best; + + return best; + } + + return tryRob(nums, 0); +}; + +assert(robRecurision2([1, 2, 3, 1]) === 4); +assert(robRecurision2([2, 7, 9, 3, 1]) === 12); + /** * 打家劫舍 DP版 * @param {number[]} nums @@ -58,3 +91,27 @@ let rob = function (nums) { assert(rob([1, 2, 3, 1]) === 4); assert(rob([2, 7, 9, 3, 1]) === 12); + +/** + * 打家劫舍 DP版2 + * @param {number[]} nums + * @return {number} + */ +let rob2 = function (nums) { + if (!nums.length) { + return 0; + } + let dp = []; + + for (let i = nums.length - 1; i >= 0; i--) { + let robNow = nums[i] + (dp[i + 2] || 0) + let robNext = dp[i + 1] || 0 + + dp[i] = Math.max(robNow, robNext) + } + + return dp[0]; +}; + + +console.log(rob2([1, 10, 3, 1, 5])); From bddd86e27d91d43e42ba2b958087cef3c7ba51ac Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 5 May 2020 13:15:20 +0800 Subject: [PATCH 036/145] =?UTF-8?q?feat:=20=E6=9C=80=E5=B0=8F=E8=8A=B1?= =?UTF-8?q?=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\347\210\254\346\245\274\346\242\257-746.js" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" new file mode 100644 index 0000000..59f9997 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" @@ -0,0 +1,16 @@ +/** + * @param {number[]} cost + * @return {number} + */ +let minCostClimbingStairs = function (cost) { + let dp = []; + + for (let i = cost.length - 1; i >= 0; i--) { + let oneStep = cost[i] + (dp[i + 1] || 0); + let twoStep = cost[i] + (dp[i + 2] || 0); + + dp[i] = Math.min(oneStep, twoStep); + } + + return Math.min(dp[0], dp[1]); +}; \ No newline at end of file From dea70e196bf9f1a0c08179c234b695a5071b3406 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 00:35:56 +0800 Subject: [PATCH 037/145] =?UTF-8?q?feat:=2001=E8=83=8C=E5=8C=85-dp?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierrc | 2 +- ...350\203\214\345\214\205-DP\347\211\210.js" | 43 ++++++++++++++++++ ...5-\351\200\222\345\275\222\347\211\210.js" | 2 +- ...06\351\200\222\345\275\222\347\211\210.js" | 44 +++++++++---------- 4 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" diff --git a/.prettierrc b/.prettierrc index 7bff574..f04c200 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,4 @@ { - "printWidth": 140, + "printWidth": 80, "tabWidth": 2 } \ No newline at end of file diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" new file mode 100644 index 0000000..4adbb48 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" @@ -0,0 +1,43 @@ +/** + * + * @param {number[]} w 物品的重量集合 + * @param {number[]} v 物品的价值集合 + * @param {number} C 背包容量 + */ +let knapsack01 = function (w, v, C) { + let n = w.length; + if (n === 0) return 0; + + // 构建二维数组dp表 + // x轴代表背包容量 y轴代表考虑的硬币情况 + // 第一行只考虑一种硬币(基准情况) + // 第二行考虑一和二两种(通过拿取二和不拿二,再去组合第一行的最佳情况来求最大值) + // 第三行以此类推 + let memo = new Array(n); + for (let i = 0; i < memo.length; i++) { + memo[i] = new Array(C + 1).fill(0); + } + + // 基础情况 背包在各个容量的情况下 只考虑第一个物品时的最优解 + for (let j = 0; j <= C; j++) { + memo[0][j] = j >= w[0] ? v[0] : 0; + } + + for (let i = 1; i < n; i++) { + for (let j = 0; j <= C; j++) { + let weight = w[i]; + let restWeight = j - weight; + // 有足够容量的情况下 选择当前的的物品 并且用剩余的重量去找前面几个物品组合的最优解 + let pickNow = j >= weight ? v[i] + memo[i - 1][restWeight] : 0; + + // 另一种选择 这个物品不放进背包了 直接求用这个背包容量组合前面几种物品的最优解 + let pickPrev = memo[i - 1][j]; + + memo[i][j] = Math.max(pickNow, pickPrev); + } + } + + return memo[n - 1][C]; +}; + +console.log(knapsack01([1, 2, 3], [6, 10, 12], 5)); diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" index c57ef28..d668e90 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-\351\200\222\345\275\222\347\211\210.js" @@ -30,4 +30,4 @@ function bestValue(w, v, index, c) { return max; } -console.log(knapsack01([1, 2, 3], [4, 5, 15], 3)); +console.log(knapsack01([1, 2, 3], [6, 10, 12], 5)); diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" index 785c067..1affffd 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\211\276\347\241\254\345\270\201-\350\256\260\345\277\206\351\200\222\345\275\222\347\211\210.js" @@ -1,28 +1,28 @@ function f(n) { function makeChange(amount) { - if(amount <= 0) return 0 - - // 校验是否已经在备忘录中存在结果,如果存在返回即可 - if(cache[amount]) return cache[amount] - - let min = Infinity - if (amount >= 1) { - min = Math.min(makeChange(amount-1) + 1, min) - } - - if (amount >= 5) { - min = Math.min(makeChange(amount-5) + 1, min) - } - - if (amount >= 11) { - min = Math.min(makeChange(amount-11) + 1, min) - } - - return (cache[amount] = min) + if (amount <= 0) return 0; + + // 校验是否已经在备忘录中存在结果,如果存在返回即可 + if (cache[amount]) return cache[amount]; + + let min = Infinity; + if (amount >= 1) { + min = Math.min(makeChange(amount - 1) + 1, min); + } + + if (amount >= 5) { + min = Math.min(makeChange(amount - 5) + 1, min); + } + + if (amount >= 11) { + min = Math.min(makeChange(amount - 11) + 1, min); + } + + return (cache[amount] = min); } // 备忘录 - const cache = [] - return makeChange(n) + const cache = []; + return makeChange(n); } -console.log(f(5000)) \ No newline at end of file +console.log(f(5000)); From d07ea5a918edadcb476a9a031661af1f2979f243 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 01:23:14 +0800 Subject: [PATCH 038/145] =?UTF-8?q?feat:=20=E5=88=86=E5=89=B2=E5=AD=90?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\222\214\345\255\220\351\233\206-416.js" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" new file mode 100644 index 0000000..95eeee0 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" @@ -0,0 +1,52 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +let canPartition = function (nums) { + let sum = nums.reduce((a, b) => a + b); + + let target = sum / 2; + if (Math.ceil(target) !== target) { + return false; + } + + let n = nums.length; + let dp = new Array(n); + for (let i = 0; i < dp.length; i++) { + dp[i] = new Array(target + 1).fill(false); + } + + for (let j = 0; j <= target; j++) { + dp[0][j] = nums[0] === target ? true : false; + } + + for (let i = 0; i < dp.length; i++) { + dp[i][0] = true; + } + + for (let i = 1; i < dp.length; i++) { + for (let j = 0; j <= target; j++) { + let num = nums[i]; + + let pick = dp[i - 1][j - num] || false; + let notPick = dp[i - 1][j] || false; + + let result = pick || notPick; + dp[i][j] = result; + + if (j == target && result) { + return true; + } + } + } + + return dp[n - 1][target]; +}; + +console.log(canPartition([1, 2, 3, 5])); + +/** + * 这个问题的思路在于,先把数组之和除以二,记为target。只要任意组合的子数组能凑成target,也就说明剩下的一定也能凑成target。 + * 1. 除以二后有小数点的直接失败,因为一定不可能是两个整数子数组相凑的结果。 + * 2. 只要用任意数量的子数组可以拼凑出来target的值,也就是dp数组的任意一层的最右边的值计算出是true,那么整题的结果就为true。因为不论你用几个值凑出了target值,哪怕只用了一个值。另外剩下的值之和一定也是target。 + */ From ab296117696e81743e71c44dd160ebd75d080d10 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 01:49:41 +0800 Subject: [PATCH 039/145] fix: typo --- .../01\350\203\214\345\214\205-DP\347\211\210.js" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" index 4adbb48..155c545 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/01\350\203\214\345\214\205-DP\347\211\210.js" @@ -9,8 +9,8 @@ let knapsack01 = function (w, v, C) { if (n === 0) return 0; // 构建二维数组dp表 - // x轴代表背包容量 y轴代表考虑的硬币情况 - // 第一行只考虑一种硬币(基准情况) + // x轴代表背包容量 y轴代表考虑的物品情况 + // 第一行只考虑一种物品(基准情况) // 第二行考虑一和二两种(通过拿取二和不拿二,再去组合第一行的最佳情况来求最大值) // 第三行以此类推 let memo = new Array(n); From 2a377ee6234d79089cde9f605be980756cbf8370 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 16:28:56 +0800 Subject: [PATCH 040/145] =?UTF-8?q?feat:=20=E6=9C=80=E5=A4=A7=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++--- ...45\255\220\346\225\260\347\273\204-152.js" | 23 +++++++++++++++++++ ...345\255\220\345\272\217\345\222\214-53.js" | 18 +++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204-152.js" create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" diff --git a/README.md b/README.md index f3f639b..d876f7e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# leetcode-javascript - -力扣的一些解题记录 +

力扣的一些解题记录 👋

+

+

## 调试 diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204-152.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204-152.js" new file mode 100644 index 0000000..6fa45df --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204-152.js" @@ -0,0 +1,23 @@ +let maxProduct = function (nums) { + let dp = []; + let n = nums.length; + + let last = nums[n - 1]; + dp[n - 1] = { + max: last, + min: last, + }; + + for (i = nums.length - 2; i >= 0; i--) { + let num = nums[i]; + let withNextMin = num * dp[i + 1].min; + let withNextMax = num * dp[i + 1].max; + let withoutNext = num; + dp[i] = { + max: Math.max(withoutNext, withNextMin, withNextMax), + min: Math.min(withoutNext, withNextMin, withNextMax), + }; + } + + return Math.max(...dp.map(({ max }) => max)); +}; diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" new file mode 100644 index 0000000..666e03a --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" @@ -0,0 +1,18 @@ +/** + * @param {number[]} nums + * @return {number} + */ +let maxSubArray = function(nums) { + let n = nums.length; + let dp = []; + + dp[n - 1] = nums[n - 1]; + + for (let i = n - 2; i >= 0; i--) { + let pickSelf = nums[i]; + let pickWithNext = pickSelf + dp[i + 1]; + dp[i] = Math.max(pickSelf, pickWithNext); + } + + return Math.max(...dp); +}; \ No newline at end of file From 4231d38535c4361f5d6367b15415cfb9fbfd1014 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 16:36:14 +0800 Subject: [PATCH 041/145] Update: README.md --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d876f7e..c2fd139 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,12 @@ -

力扣的一些解题记录 👋

+

Welcome to leetcode-javascript 👋

+ + License: MIT +

+> 力扣的题解记录(JavaScript) + ## 调试 提供了 .vscode 配置文件,在 vscode 中选择「小爬虫」图标,点击启动程序,即可启动断点调试。 @@ -9,3 +14,22 @@ ## 思路 思路会记录在本仓库的 Issues 中,按照 label 进行分类。比如想查看 「DFS」 分类下的问题,那么选择标签进行筛选即可。 + +## Author + +👤 **ssh** + +- Website: https://ssh-blog.now.sh +- Github: [@sl1673495](https://github.com/sl1673495) + +## 🤝 Contributing + +Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/sl1673495/leetcode-javascript/issues). + +## Show your support + +Give a ⭐️ if this project helped you! + +--- + +_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ From a4d5b98e454e3570e8851572734cb87b39ad44c7 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 16:53:02 +0800 Subject: [PATCH 042/145] fix: remove report json --- report.20200503.211357.57008.0.001.json | 453 ------------------------ report.20200503.211524.56860.0.001.json | 453 ------------------------ 2 files changed, 906 deletions(-) delete mode 100644 report.20200503.211357.57008.0.001.json delete mode 100644 report.20200503.211524.56860.0.001.json diff --git a/report.20200503.211357.57008.0.001.json b/report.20200503.211357.57008.0.001.json deleted file mode 100644 index 1fde733..0000000 --- a/report.20200503.211357.57008.0.001.json +++ /dev/null @@ -1,453 +0,0 @@ - -{ - "header": { - "reportVersion": 1, - "event": "Allocation failed - JavaScript heap out of memory", - "trigger": "FatalError", - "filename": "report.20200503.211357.57008.0.001.json", - "dumpEventTime": "2020-05-03T21:13:57Z", - "dumpEventTimeStamp": "1588511637443", - "processId": 57008, - "cwd": "c:\\codes\\leetcode-javascript", - "commandLine": [ - "node", - "c:\\codes\\leetcode-javascript\\动态规划\\打家劫舍III-337.js" - ], - "nodejsVersion": "v12.13.1", - "wordSize": 64, - "arch": "x64", - "platform": "win32", - "componentVersions": { - "node": "12.13.1", - "v8": "7.7.299.13-node.16", - "uv": "1.33.1", - "zlib": "1.2.11", - "brotli": "1.0.7", - "ares": "1.15.0", - "modules": "72", - "nghttp2": "1.39.2", - "napi": "5", - "llhttp": "1.1.4", - "http_parser": "2.8.0", - "openssl": "1.1.1d", - "cldr": "35.1", - "icu": "64.2", - "tz": "2019c", - "unicode": "12.1" - }, - "release": { - "name": "node", - "lts": "Erbium", - "headersUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1-headers.tar.gz", - "sourceUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1.tar.gz", - "libUrl": "https://nodejs.org/download/release/v12.13.1/win-x64/node.lib" - }, - "osName": "Windows_NT", - "osRelease": "10.0.18362", - "osVersion": "Windows 10 Home China", - "osMachine": "x86_64", - "cpus": [ - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3279531, - "nice": 0, - "sys": 2751609, - "idle": 140155812, - "irq": 603109 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1231875, - "nice": 0, - "sys": 818218, - "idle": 144136328, - "irq": 66671 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 5152671, - "nice": 0, - "sys": 2923187, - "idle": 138110562, - "irq": 71500 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 2180406, - "nice": 0, - "sys": 959343, - "idle": 143046671, - "irq": 22125 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3605000, - "nice": 0, - "sys": 2255015, - "idle": 140326406, - "irq": 55812 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1492312, - "nice": 0, - "sys": 883390, - "idle": 143810718, - "irq": 22812 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3256843, - "nice": 0, - "sys": 1774250, - "idle": 141155312, - "irq": 50312 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 2527296, - "nice": 0, - "sys": 1056890, - "idle": 142602218, - "irq": 35937 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3385750, - "nice": 0, - "sys": 2304171, - "idle": 140496484, - "irq": 64593 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1167796, - "nice": 0, - "sys": 931109, - "idle": 144087500, - "irq": 14453 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3003156, - "nice": 0, - "sys": 1937562, - "idle": 141245687, - "irq": 55984 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3453328, - "nice": 0, - "sys": 1274593, - "idle": 141458484, - "irq": 24015 - } - ], - "networkInterfaces": [ - { - "name": "Loopback Pseudo-Interface 1", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "::1", - "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - "family": "IPv6", - "scopeid": 0 - }, - { - "name": "Loopback Pseudo-Interface 1", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "127.0.0.1", - "netmask": "255.0.0.0", - "family": "IPv4" - }, - { - "name": "WLAN", - "internal": false, - "mac": "50:e0:85:2f:fa:3a", - "address": "172.20.10.4", - "netmask": "255.255.255.240", - "family": "IPv4" - } - ], - "host": "LAPTOP-FMHNK1K6" - }, - "javascriptStack": { - "message": "No stack.", - "stack": [ - "Unavailable." - ] - }, - "nativeStack": [ - { - "pc": "0x00007ff6a40f1729", - "symbol": "std::basic_ostream >::operator<<+10873" - }, - { - "pc": "0x00007ff6a40f5b4c", - "symbol": "std::basic_ostream >::operator<<+28316" - }, - { - "pc": "0x00007ff6a40f4b08", - "symbol": "std::basic_ostream >::operator<<+24152" - }, - { - "pc": "0x00007ff6a41e369b", - "symbol": "v8::base::CPU::has_sse+37723" - }, - { - "pc": "0x00007ff6a49e82de", - "symbol": "v8::Isolate::ReportExternalAllocationLimitReached+94" - }, - { - "pc": "0x00007ff6a49d0321", - "symbol": "v8::SharedArrayBuffer::Externalize+833" - }, - { - "pc": "0x00007ff6a489dbec", - "symbol": "v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436" - }, - { - "pc": "0x00007ff6a48a8f90", - "symbol": "v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312" - }, - { - "pc": "0x00007ff6a48a5ac4", - "symbol": "v8::internal::Heap::PageFlagsAreConsistent+3204" - }, - { - "pc": "0x00007ff6a489b353", - "symbol": "v8::internal::Heap::CollectGarbage+1283" - }, - { - "pc": "0x00007ff6a4899b24", - "symbol": "v8::internal::Heap::AddRetainedMap+2356" - }, - { - "pc": "0x00007ff6a48b3c2e", - "symbol": "v8::internal::Factory::AllocateRawFixedArray+94" - }, - { - "pc": "0x00007ff6a48bafc4", - "symbol": "v8::internal::Factory::NewFixedArrayWithFiller+52" - }, - { - "pc": "0x00007ff6a48baf81", - "symbol": "v8::internal::Factory::NewUninitializedFixedArray+65" - }, - { - "pc": "0x00007ff6a479bdaf", - "symbol": "v8::Object::GetIsolate+6767" - }, - { - "pc": "0x00007ff6a464f13a", - "symbol": "v8::internal::OrderedHashMap::ValueAt+61274" - }, - { - "pc": "0x00007ff6a4e1404d", - "symbol": "v8::internal::SetupIsolateDelegate::SetupHeap+567949" - }, - { - "pc": "0x000002018f08794d", - "symbol": "" - } - ], - "javascriptHeap": { - "totalMemory": 2193383424, - "totalCommittedMemory": 2193383424, - "usedMemory": 2081107136, - "availableMemory": 42146160, - "memoryLimit": 2197815296, - "heapSpaces": { - "read_only_space": { - "memorySize": 262144, - "committedMemory": 262144, - "capacity": 261872, - "used": 32296, - "available": 229576 - }, - "new_space": { - "memorySize": 33554432, - "committedMemory": 33554432, - "capacity": 16759808, - "used": 32864, - "available": 16726944 - }, - "old_space": { - "memorySize": 599371776, - "committedMemory": 599371776, - "capacity": 549298736, - "used": 545300776, - "available": 3997960 - }, - "code_space": { - "memorySize": 425984, - "committedMemory": 425984, - "capacity": 160896, - "used": 160896, - "available": 0 - }, - "map_space": { - "memorySize": 266240, - "committedMemory": 266240, - "capacity": 178080, - "used": 178080, - "available": 0 - }, - "large_object_space": { - "memorySize": 1559453696, - "committedMemory": 1559453696, - "capacity": 1535398672, - "used": 1535398672, - "available": 0 - }, - "code_large_object_space": { - "memorySize": 49152, - "committedMemory": 49152, - "capacity": 3552, - "used": 3552, - "available": 0 - }, - "new_large_object_space": { - "memorySize": 0, - "committedMemory": 0, - "capacity": 16759808, - "used": 0, - "available": 16759808 - } - } - }, - "resourceUsage": { - "userCpuSeconds": 8.312, - "kernelCpuSeconds": 1.625, - "cpuConsumptionPercent": 141.957, - "maxRss": 2224611328, - "pageFaults": { - "IORequired": 722070, - "IONotRequired": 0 - }, - "fsActivity": { - "reads": 2, - "writes": 110 - } - }, - "libuv": [ - ], - "environmentVariables": { - "=C:": "c:\\codes\\leetcode-javascript", - "ALLUSERSPROFILE": "C:\\ProgramData", - "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", - "APPDATA": "C:\\Users\\94958\\AppData\\Roaming", - "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", - "CommonProgramFiles": "C:\\Program Files\\Common Files", - "CommonProgramFiles(x86)": "C:\\Program Files (x86)\\Common Files", - "CommonProgramW6432": "C:\\Program Files\\Common Files", - "COMPUTERNAME": "LAPTOP-FMHNK1K6", - "ComSpec": "C:\\Windows\\system32\\cmd.exe", - "DriverData": "C:\\Windows\\System32\\Drivers\\DriverData", - "ELECTRON_RUN_AS_NODE": "1", - "GameFirst": "C:\\Program Files (x86)\\ASUS\\GameFirst\\", - "GameFirstUserPath": "C:\\Users\\94958\\AppData\\Local\\ASUS\\GameFirst", - "HOMEDRIVE": "C:", - "HOMEPATH": "\\Users\\94958", - "LOCALAPPDATA": "C:\\Users\\94958\\AppData\\Local", - "LOGONSERVER": "\\\\LAPTOP-FMHNK1K6", - "NUMBER_OF_PROCESSORS": "12", - "OneDrive": "C:\\Users\\94958\\OneDrive", - "OneDriveConsumer": "C:\\Users\\94958\\OneDrive", - "OS": "Windows_NT", - "Path": "C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\nodejs\\;C:\\Users\\94958\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\94958\\AppData\\Roaming\\npm;C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", - "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", - "PIPE_LOGGING": "true", - "PROCESSOR_ARCHITECTURE": "AMD64", - "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 10, GenuineIntel", - "PROCESSOR_LEVEL": "6", - "PROCESSOR_REVISION": "9e0a", - "ProgramData": "C:\\ProgramData", - "ProgramFiles": "C:\\Program Files", - "ProgramFiles(x86)": "C:\\Program Files (x86)", - "ProgramW6432": "C:\\Program Files", - "PROMPT": "$P$G", - "PSModulePath": "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules", - "PUBLIC": "C:\\Users\\Public", - "SESSIONNAME": "Console", - "SystemDrive": "C:", - "SystemRoot": "C:\\Windows", - "TEMP": "C:\\Users\\94958\\AppData\\Local\\Temp", - "TMP": "C:\\Users\\94958\\AppData\\Local\\Temp", - "USERDOMAIN": "LAPTOP-FMHNK1K6", - "USERDOMAIN_ROAMINGPROFILE": "LAPTOP-FMHNK1K6", - "USERNAME": "94958", - "USERPROFILE": "C:\\Users\\94958", - "VERBOSE_LOGGING": "true", - "VSCODE_CWD": "C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code", - "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", - "VSCODE_IPC_HOOK": "\\\\.\\pipe\\b8cbe1eb44c59330f23b7126ffc1d83e-1.44.2-main-sock", - "VSCODE_IPC_HOOK_EXTHOST": "\\\\.\\pipe\\vscode-ipc-ca5d424d-f130-4cc5-b06e-3552ea0b7394-sock", - "VSCODE_LOGS": "C:\\Users\\94958\\AppData\\Roaming\\Code\\logs\\20200503T205835", - "VSCODE_LOG_STACK": "false", - "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_translationsConfigFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\tcf.json\",\"_cacheRoot\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\ff915844119ce9485abfe8aa9076ec76b5300ddd\",\"_corruptedFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\corrupted.info\",\"_languagePackSupport\":true}", - "VSCODE_NODE_CACHED_DATA_DIR": "C:\\Users\\94958\\AppData\\Roaming\\Code\\CachedData\\ff915844119ce9485abfe8aa9076ec76b5300ddd", - "VSCODE_PID": "56608", - "windir": "C:\\Windows" - }, - "sharedObjects": [ - "C:\\Program Files\\nodejs\\node.exe", - "C:\\Windows\\SYSTEM32\\ntdll.dll", - "C:\\Windows\\System32\\KERNEL32.DLL", - "C:\\Windows\\System32\\KERNELBASE.dll", - "C:\\Windows\\System32\\WS2_32.dll", - "C:\\Windows\\System32\\RPCRT4.dll", - "C:\\Windows\\System32\\ADVAPI32.dll", - "C:\\Windows\\SYSTEM32\\dbghelp.dll", - "C:\\Windows\\System32\\msvcrt.dll", - "C:\\Windows\\System32\\ucrtbase.dll", - "C:\\Windows\\System32\\sechost.dll", - "C:\\Windows\\System32\\USER32.dll", - "C:\\Windows\\System32\\win32u.dll", - "C:\\Windows\\System32\\GDI32.dll", - "C:\\Windows\\System32\\gdi32full.dll", - "C:\\Windows\\System32\\msvcp_win.dll", - "C:\\Windows\\System32\\PSAPI.DLL", - "C:\\Windows\\System32\\CRYPT32.dll", - "C:\\Windows\\System32\\MSASN1.dll", - "C:\\Windows\\SYSTEM32\\IPHLPAPI.DLL", - "C:\\Windows\\System32\\bcrypt.dll", - "C:\\Windows\\SYSTEM32\\USERENV.dll", - "C:\\Windows\\System32\\profapi.dll", - "C:\\Windows\\SYSTEM32\\WINMM.dll", - "C:\\Windows\\SYSTEM32\\winmmbase.dll", - "C:\\Windows\\System32\\cfgmgr32.dll", - "C:\\Windows\\System32\\bcryptPrimitives.dll", - "C:\\Windows\\System32\\IMM32.DLL", - "C:\\Windows\\System32\\powrprof.dll", - "C:\\Windows\\System32\\UMPDC.dll", - "C:\\Windows\\SYSTEM32\\CRYPTBASE.DLL", - "C:\\Windows\\system32\\uxtheme.dll", - "C:\\Windows\\System32\\combase.dll", - "C:\\Windows\\system32\\mswsock.dll", - "C:\\Windows\\System32\\kernel.appcore.dll", - "C:\\Windows\\System32\\NSI.dll", - "C:\\Windows\\SYSTEM32\\dhcpcsvc6.DLL", - "C:\\Windows\\SYSTEM32\\dhcpcsvc.DLL", - "C:\\Windows\\SYSTEM32\\DNSAPI.dll", - "C:\\Windows\\system32\\napinsp.dll", - "C:\\Windows\\system32\\pnrpnsp.dll", - "C:\\Windows\\System32\\winrnr.dll", - "C:\\Windows\\system32\\NLAapi.dll", - "C:\\Windows\\system32\\wshbth.dll" - ] -} \ No newline at end of file diff --git a/report.20200503.211524.56860.0.001.json b/report.20200503.211524.56860.0.001.json deleted file mode 100644 index 4131636..0000000 --- a/report.20200503.211524.56860.0.001.json +++ /dev/null @@ -1,453 +0,0 @@ - -{ - "header": { - "reportVersion": 1, - "event": "Allocation failed - JavaScript heap out of memory", - "trigger": "FatalError", - "filename": "report.20200503.211524.56860.0.001.json", - "dumpEventTime": "2020-05-03T21:15:24Z", - "dumpEventTimeStamp": "1588511724189", - "processId": 56860, - "cwd": "c:\\codes\\leetcode-javascript", - "commandLine": [ - "node", - "c:\\codes\\leetcode-javascript\\动态规划\\打家劫舍III-337.js" - ], - "nodejsVersion": "v12.13.1", - "wordSize": 64, - "arch": "x64", - "platform": "win32", - "componentVersions": { - "node": "12.13.1", - "v8": "7.7.299.13-node.16", - "uv": "1.33.1", - "zlib": "1.2.11", - "brotli": "1.0.7", - "ares": "1.15.0", - "modules": "72", - "nghttp2": "1.39.2", - "napi": "5", - "llhttp": "1.1.4", - "http_parser": "2.8.0", - "openssl": "1.1.1d", - "cldr": "35.1", - "icu": "64.2", - "tz": "2019c", - "unicode": "12.1" - }, - "release": { - "name": "node", - "lts": "Erbium", - "headersUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1-headers.tar.gz", - "sourceUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1.tar.gz", - "libUrl": "https://nodejs.org/download/release/v12.13.1/win-x64/node.lib" - }, - "osName": "Windows_NT", - "osRelease": "10.0.18362", - "osVersion": "Windows 10 Home China", - "osMachine": "x86_64", - "cpus": [ - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3296453, - "nice": 0, - "sys": 2762812, - "idle": 140214437, - "irq": 605781 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1244515, - "nice": 0, - "sys": 821750, - "idle": 144206906, - "irq": 67578 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 5176453, - "nice": 0, - "sys": 2930281, - "idle": 138166437, - "irq": 71828 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 2193328, - "nice": 0, - "sys": 963750, - "idle": 143116093, - "irq": 22359 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3621390, - "nice": 0, - "sys": 2261281, - "idle": 140390500, - "irq": 56046 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1504703, - "nice": 0, - "sys": 886625, - "idle": 143881843, - "irq": 22859 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3272625, - "nice": 0, - "sys": 1780046, - "idle": 141220484, - "irq": 50531 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 2539703, - "nice": 0, - "sys": 1061375, - "idle": 142672078, - "irq": 36078 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3401375, - "nice": 0, - "sys": 2311562, - "idle": 140560218, - "irq": 64843 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1174734, - "nice": 0, - "sys": 933906, - "idle": 144164515, - "irq": 14515 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3018156, - "nice": 0, - "sys": 1943500, - "idle": 141311500, - "irq": 56203 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 3469390, - "nice": 0, - "sys": 1280562, - "idle": 141523203, - "irq": 24203 - } - ], - "networkInterfaces": [ - { - "name": "Loopback Pseudo-Interface 1", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "::1", - "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - "family": "IPv6", - "scopeid": 0 - }, - { - "name": "Loopback Pseudo-Interface 1", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "127.0.0.1", - "netmask": "255.0.0.0", - "family": "IPv4" - }, - { - "name": "WLAN", - "internal": false, - "mac": "50:e0:85:2f:fa:3a", - "address": "172.20.10.4", - "netmask": "255.255.255.240", - "family": "IPv4" - } - ], - "host": "LAPTOP-FMHNK1K6" - }, - "javascriptStack": { - "message": "No stack.", - "stack": [ - "Unavailable." - ] - }, - "nativeStack": [ - { - "pc": "0x00007ff6a40f1729", - "symbol": "std::basic_ostream >::operator<<+10873" - }, - { - "pc": "0x00007ff6a40f5b4c", - "symbol": "std::basic_ostream >::operator<<+28316" - }, - { - "pc": "0x00007ff6a40f4b08", - "symbol": "std::basic_ostream >::operator<<+24152" - }, - { - "pc": "0x00007ff6a41e369b", - "symbol": "v8::base::CPU::has_sse+37723" - }, - { - "pc": "0x00007ff6a49e82de", - "symbol": "v8::Isolate::ReportExternalAllocationLimitReached+94" - }, - { - "pc": "0x00007ff6a49d0321", - "symbol": "v8::SharedArrayBuffer::Externalize+833" - }, - { - "pc": "0x00007ff6a489dbec", - "symbol": "v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436" - }, - { - "pc": "0x00007ff6a48a8f90", - "symbol": "v8::internal::Heap::ProtectUnprotectedMemoryChunks+1312" - }, - { - "pc": "0x00007ff6a48a5ac4", - "symbol": "v8::internal::Heap::PageFlagsAreConsistent+3204" - }, - { - "pc": "0x00007ff6a489b353", - "symbol": "v8::internal::Heap::CollectGarbage+1283" - }, - { - "pc": "0x00007ff6a4899b24", - "symbol": "v8::internal::Heap::AddRetainedMap+2356" - }, - { - "pc": "0x00007ff6a48b3c2e", - "symbol": "v8::internal::Factory::AllocateRawFixedArray+94" - }, - { - "pc": "0x00007ff6a48bafc4", - "symbol": "v8::internal::Factory::NewFixedArrayWithFiller+52" - }, - { - "pc": "0x00007ff6a48baf81", - "symbol": "v8::internal::Factory::NewUninitializedFixedArray+65" - }, - { - "pc": "0x00007ff6a479bdaf", - "symbol": "v8::Object::GetIsolate+6767" - }, - { - "pc": "0x00007ff6a464f13a", - "symbol": "v8::internal::OrderedHashMap::ValueAt+61274" - }, - { - "pc": "0x00007ff6a4e1404d", - "symbol": "v8::internal::SetupIsolateDelegate::SetupHeap+567949" - }, - { - "pc": "0x0000005906d4794d", - "symbol": "" - } - ], - "javascriptHeap": { - "totalMemory": 2194755584, - "totalCommittedMemory": 2194755584, - "usedMemory": 2082495824, - "availableMemory": 39710424, - "memoryLimit": 2197815296, - "heapSpaces": { - "read_only_space": { - "memorySize": 262144, - "committedMemory": 262144, - "capacity": 261872, - "used": 32296, - "available": 229576 - }, - "new_space": { - "memorySize": 33554432, - "committedMemory": 33554432, - "capacity": 16759808, - "used": 65568, - "available": 16694240 - }, - "old_space": { - "memorySize": 599371776, - "committedMemory": 599371776, - "capacity": 548268088, - "used": 545301000, - "available": 2967088 - }, - "code_space": { - "memorySize": 425984, - "committedMemory": 425984, - "capacity": 160896, - "used": 160896, - "available": 0 - }, - "map_space": { - "memorySize": 266240, - "committedMemory": 266240, - "capacity": 178080, - "used": 178080, - "available": 0 - }, - "large_object_space": { - "memorySize": 1560825856, - "committedMemory": 1560825856, - "capacity": 1536754432, - "used": 1536754432, - "available": 0 - }, - "code_large_object_space": { - "memorySize": 49152, - "committedMemory": 49152, - "capacity": 3552, - "used": 3552, - "available": 0 - }, - "new_large_object_space": { - "memorySize": 0, - "committedMemory": 0, - "capacity": 16759808, - "used": 0, - "available": 16759808 - } - } - }, - "resourceUsage": { - "userCpuSeconds": 10.656, - "kernelCpuSeconds": 1.5, - "cpuConsumptionPercent": 151.95, - "maxRss": 2226262016, - "pageFaults": { - "IORequired": 722857, - "IONotRequired": 0 - }, - "fsActivity": { - "reads": 2, - "writes": 110 - } - }, - "libuv": [ - ], - "environmentVariables": { - "=C:": "c:\\codes\\leetcode-javascript", - "ALLUSERSPROFILE": "C:\\ProgramData", - "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", - "APPDATA": "C:\\Users\\94958\\AppData\\Roaming", - "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", - "CommonProgramFiles": "C:\\Program Files\\Common Files", - "CommonProgramFiles(x86)": "C:\\Program Files (x86)\\Common Files", - "CommonProgramW6432": "C:\\Program Files\\Common Files", - "COMPUTERNAME": "LAPTOP-FMHNK1K6", - "ComSpec": "C:\\Windows\\system32\\cmd.exe", - "DriverData": "C:\\Windows\\System32\\Drivers\\DriverData", - "ELECTRON_RUN_AS_NODE": "1", - "GameFirst": "C:\\Program Files (x86)\\ASUS\\GameFirst\\", - "GameFirstUserPath": "C:\\Users\\94958\\AppData\\Local\\ASUS\\GameFirst", - "HOMEDRIVE": "C:", - "HOMEPATH": "\\Users\\94958", - "LOCALAPPDATA": "C:\\Users\\94958\\AppData\\Local", - "LOGONSERVER": "\\\\LAPTOP-FMHNK1K6", - "NUMBER_OF_PROCESSORS": "12", - "OneDrive": "C:\\Users\\94958\\OneDrive", - "OneDriveConsumer": "C:\\Users\\94958\\OneDrive", - "OS": "Windows_NT", - "Path": "C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\nodejs\\;C:\\Users\\94958\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\94958\\AppData\\Roaming\\npm;C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", - "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", - "PIPE_LOGGING": "true", - "PROCESSOR_ARCHITECTURE": "AMD64", - "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 10, GenuineIntel", - "PROCESSOR_LEVEL": "6", - "PROCESSOR_REVISION": "9e0a", - "ProgramData": "C:\\ProgramData", - "ProgramFiles": "C:\\Program Files", - "ProgramFiles(x86)": "C:\\Program Files (x86)", - "ProgramW6432": "C:\\Program Files", - "PROMPT": "$P$G", - "PSModulePath": "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules", - "PUBLIC": "C:\\Users\\Public", - "SESSIONNAME": "Console", - "SystemDrive": "C:", - "SystemRoot": "C:\\Windows", - "TEMP": "C:\\Users\\94958\\AppData\\Local\\Temp", - "TMP": "C:\\Users\\94958\\AppData\\Local\\Temp", - "USERDOMAIN": "LAPTOP-FMHNK1K6", - "USERDOMAIN_ROAMINGPROFILE": "LAPTOP-FMHNK1K6", - "USERNAME": "94958", - "USERPROFILE": "C:\\Users\\94958", - "VERBOSE_LOGGING": "true", - "VSCODE_CWD": "C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code", - "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", - "VSCODE_IPC_HOOK": "\\\\.\\pipe\\b8cbe1eb44c59330f23b7126ffc1d83e-1.44.2-main-sock", - "VSCODE_IPC_HOOK_EXTHOST": "\\\\.\\pipe\\vscode-ipc-ca5d424d-f130-4cc5-b06e-3552ea0b7394-sock", - "VSCODE_LOGS": "C:\\Users\\94958\\AppData\\Roaming\\Code\\logs\\20200503T205835", - "VSCODE_LOG_STACK": "false", - "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_translationsConfigFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\tcf.json\",\"_cacheRoot\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\ff915844119ce9485abfe8aa9076ec76b5300ddd\",\"_corruptedFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\\\\corrupted.info\",\"_languagePackSupport\":true}", - "VSCODE_NODE_CACHED_DATA_DIR": "C:\\Users\\94958\\AppData\\Roaming\\Code\\CachedData\\ff915844119ce9485abfe8aa9076ec76b5300ddd", - "VSCODE_PID": "56608", - "windir": "C:\\Windows" - }, - "sharedObjects": [ - "C:\\Program Files\\nodejs\\node.exe", - "C:\\Windows\\SYSTEM32\\ntdll.dll", - "C:\\Windows\\System32\\KERNEL32.DLL", - "C:\\Windows\\System32\\KERNELBASE.dll", - "C:\\Windows\\System32\\WS2_32.dll", - "C:\\Windows\\System32\\RPCRT4.dll", - "C:\\Windows\\SYSTEM32\\dbghelp.dll", - "C:\\Windows\\System32\\ADVAPI32.dll", - "C:\\Windows\\System32\\msvcrt.dll", - "C:\\Windows\\System32\\ucrtbase.dll", - "C:\\Windows\\System32\\sechost.dll", - "C:\\Windows\\System32\\USER32.dll", - "C:\\Windows\\System32\\win32u.dll", - "C:\\Windows\\System32\\GDI32.dll", - "C:\\Windows\\System32\\gdi32full.dll", - "C:\\Windows\\System32\\msvcp_win.dll", - "C:\\Windows\\System32\\PSAPI.DLL", - "C:\\Windows\\System32\\CRYPT32.dll", - "C:\\Windows\\System32\\MSASN1.dll", - "C:\\Windows\\System32\\bcrypt.dll", - "C:\\Windows\\SYSTEM32\\IPHLPAPI.DLL", - "C:\\Windows\\SYSTEM32\\USERENV.dll", - "C:\\Windows\\System32\\profapi.dll", - "C:\\Windows\\SYSTEM32\\WINMM.dll", - "C:\\Windows\\SYSTEM32\\winmmbase.dll", - "C:\\Windows\\System32\\cfgmgr32.dll", - "C:\\Windows\\System32\\bcryptPrimitives.dll", - "C:\\Windows\\System32\\IMM32.DLL", - "C:\\Windows\\System32\\powrprof.dll", - "C:\\Windows\\System32\\UMPDC.dll", - "C:\\Windows\\SYSTEM32\\CRYPTBASE.DLL", - "C:\\Windows\\system32\\uxtheme.dll", - "C:\\Windows\\System32\\combase.dll", - "C:\\Windows\\system32\\mswsock.dll", - "C:\\Windows\\System32\\kernel.appcore.dll", - "C:\\Windows\\System32\\NSI.dll", - "C:\\Windows\\SYSTEM32\\dhcpcsvc6.DLL", - "C:\\Windows\\SYSTEM32\\dhcpcsvc.DLL", - "C:\\Windows\\SYSTEM32\\DNSAPI.dll", - "C:\\Windows\\system32\\napinsp.dll", - "C:\\Windows\\system32\\pnrpnsp.dll", - "C:\\Windows\\System32\\winrnr.dll", - "C:\\Windows\\system32\\NLAapi.dll", - "C:\\Windows\\system32\\wshbth.dll" - ] -} \ No newline at end of file From 960b95120f42059fb0f99cc22094f95d5ba82a06 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 6 May 2020 20:26:40 +0800 Subject: [PATCH 043/145] =?UTF-8?q?feat:=20=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\344\275\263\346\227\266\346\234\272.js" | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.js" new file mode 100644 index 0000000..5c5d751 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.js" @@ -0,0 +1,44 @@ + + +/** + * 循环版 + * @param {number[]} prices + * @return {number} + */ +let maxProfit = function(prices) { + let max = 0 + for (let i = 1; i < prices.length; i++) { + for (let j = 0; j < i; j++) { + let price = prices[j] + let sale = prices[i] - price + max = Math.max(max, sale) + } + } + + return max +}; + + +/** + * DP版 + */ +let maxProfit = function (prices) { + let n = prices.length + if (!n || n === 1) return 0 + + // 最大收益 + let prevMax = 0 + // 最小价格 + let prevMin = prices[0] + + for (let i = 1; i < n; i++) { + let price = prices[i] + + prevMax = Math.max(price - prevMin, prevMax) + prevMin = Math.min(price, prevMin) + } + + return prevMax +}; + +console.log(maxProfit([1, 2])) \ No newline at end of file From 131bb2962f0edcb50daf10f16f987e67da78c1e2 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 7 May 2020 19:16:17 +0800 Subject: [PATCH 044/145] =?UTF-8?q?feat:=20=E8=BF=9B=E4=B8=80=E6=AD=A5?= =?UTF-8?q?=E7=90=86=E8=A7=A3=E5=88=86=E9=9A=94=E7=AD=89=E5=92=8C=E5=AD=90?= =?UTF-8?q?=E9=9B=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- report.20200507.190200.31578.0.001.json | 584 ++++++++++++++++++ ...45\222\214\345\255\220\351\233\206-416.js" | 53 +- ...45\245\221\346\225\260\345\210\227-509.js" | 25 +- 3 files changed, 629 insertions(+), 33 deletions(-) create mode 100644 report.20200507.190200.31578.0.001.json diff --git a/report.20200507.190200.31578.0.001.json b/report.20200507.190200.31578.0.001.json new file mode 100644 index 0000000..83e1ea5 --- /dev/null +++ b/report.20200507.190200.31578.0.001.json @@ -0,0 +1,584 @@ + +{ + "header": { + "reportVersion": 1, + "event": "Allocation failed - JavaScript heap out of memory", + "trigger": "FatalError", + "filename": "report.20200507.190200.31578.0.001.json", + "dumpEventTime": "2020-05-07T19:02:00Z", + "dumpEventTimeStamp": "1588849320349", + "processId": 31578, + "cwd": "/Users/shanshihao/code/leetcode-javascript", + "commandLine": [ + "node", + "/Users/shanshihao/code/leetcode-javascript/动态规划/分割等和子集-416.js" + ], + "nodejsVersion": "v12.14.1", + "wordSize": 64, + "arch": "x64", + "platform": "darwin", + "componentVersions": { + "node": "12.14.1", + "v8": "7.7.299.13-node.16", + "uv": "1.33.1", + "zlib": "1.2.11", + "brotli": "1.0.7", + "ares": "1.15.0", + "modules": "72", + "nghttp2": "1.40.0", + "napi": "5", + "llhttp": "2.0.1", + "http_parser": "2.8.0", + "openssl": "1.1.1d", + "cldr": "35.1", + "icu": "64.2", + "tz": "2019c", + "unicode": "12.1" + }, + "release": { + "name": "node", + "lts": "Erbium", + "headersUrl": "https://nodejs.org/download/release/v12.14.1/node-v12.14.1-headers.tar.gz", + "sourceUrl": "https://nodejs.org/download/release/v12.14.1/node-v12.14.1.tar.gz" + }, + "osName": "Darwin", + "osRelease": "18.5.0", + "osVersion": "Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64", + "osMachine": "x86_64", + "cpus": [ + { + "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", + "speed": 2300, + "user": 212947840, + "nice": 0, + "sys": 149762140, + "idle": 576349080, + "irq": 0 + }, + { + "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", + "speed": 2300, + "user": 148908440, + "nice": 0, + "sys": 77507840, + "idle": 712452200, + "irq": 0 + }, + { + "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", + "speed": 2300, + "user": 214894110, + "nice": 0, + "sys": 132663920, + "idle": 591310940, + "irq": 0 + }, + { + "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", + "speed": 2300, + "user": 132767920, + "nice": 0, + "sys": 63910290, + "idle": 742190080, + "irq": 0 + } + ], + "networkInterfaces": [ + { + "name": "lo0", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "127.0.0.1", + "netmask": "255.0.0.0", + "family": "IPv4" + }, + { + "name": "lo0", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "::1", + "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", + "family": "IPv6", + "scopeid": 0 + }, + { + "name": "lo0", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "fe80::1", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 1 + }, + { + "name": "en0", + "internal": false, + "mac": "88:e9:fe:50:9c:d2", + "address": "fe80::18b9:7632:9a0e:831e", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 6 + }, + { + "name": "en0", + "internal": false, + "mac": "88:e9:fe:50:9c:d2", + "address": "30.43.84.43", + "netmask": "255.255.240.0", + "family": "IPv4" + }, + { + "name": "awdl0", + "internal": false, + "mac": "6e:5a:0e:89:b0:73", + "address": "fe80::6c5a:eff:fe89:b073", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 8 + }, + { + "name": "utun0", + "internal": false, + "mac": "00:00:00:00:00:00", + "address": "fe80::17be:efd0:e58e:24c2", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 12 + }, + { + "name": "utun1", + "internal": false, + "mac": "00:00:00:00:00:00", + "address": "fe80::847c:7463:1209:7935", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 13 + }, + { + "name": "utun2", + "internal": false, + "mac": "00:00:00:00:00:00", + "address": "fe80::cc27:fa71:3472:6bed", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 14 + }, + { + "name": "utun3", + "internal": false, + "mac": "00:00:00:00:00:00", + "address": "fe80::f4f:d641:8cbd:6df3", + "netmask": "ffff:ffff:ffff:ffff::", + "family": "IPv6", + "scopeid": 15 + } + ], + "host": "shanshihaodeMacBook-Pro.local" + }, + "javascriptStack": { + "message": "No stack.", + "stack": [ + "Unavailable." + ] + }, + "nativeStack": [ + { + "pc": "0x000000010014e0ce", + "symbol": "report::TriggerNodeReport(v8::Isolate*, node::Environment*, char const*, char const*, std::__1::basic_string, std::__1::allocator > const&, v8::Local) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x000000010007f391", + "symbol": "node::OnFatalError(char const*, char const*) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x0000000100176887", + "symbol": "v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x0000000100176823", + "symbol": "v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x00000001002fa9d5", + "symbol": "v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x00000001002ce0b1", + "symbol": "v8::internal::Factory::NewFixedArrayWithFiller(v8::internal::RootIndex, int, v8::internal::Object, v8::internal::AllocationType) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x0000000100439d5a", + "symbol": "v8::internal::(anonymous namespace)::ElementsAccessorBase >::GrowCapacity(v8::internal::Handle, unsigned int) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x00000001005d2f04", + "symbol": "v8::internal::Runtime_GrowArrayElements(int, unsigned long*, v8::internal::Isolate*) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + }, + { + "pc": "0x00000001009311f9", + "symbol": "Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" + } + ], + "javascriptHeap": { + "totalMemory": 2269442048, + "totalCommittedMemory": 2237021920, + "usedMemory": 2234924776, + "availableMemory": 17192408, + "memoryLimit": 2197815296, + "heapSpaces": { + "read_only_space": { + "memorySize": 262144, + "committedMemory": 32568, + "capacity": 261872, + "used": 32296, + "available": 229576 + }, + "new_space": { + "memorySize": 33554432, + "committedMemory": 1981616, + "capacity": 16759808, + "used": 0, + "available": 16759808 + }, + "old_space": { + "memorySize": 1945600, + "committedMemory": 1594216, + "capacity": 1795064, + "used": 1592040, + "available": 203024 + }, + "code_space": { + "memorySize": 425984, + "committedMemory": 176128, + "capacity": 159744, + "used": 159744, + "available": 0 + }, + "map_space": { + "memorySize": 266240, + "committedMemory": 249744, + "capacity": 249200, + "used": 249200, + "available": 0 + }, + "large_object_space": { + "memorySize": 1488248832, + "committedMemory": 1488248832, + "capacity": 1488202072, + "used": 1488202072, + "available": 0 + }, + "code_large_object_space": { + "memorySize": 49152, + "committedMemory": 49152, + "capacity": 3552, + "used": 3552, + "available": 0 + }, + "new_large_object_space": { + "memorySize": 744689664, + "committedMemory": 744689664, + "capacity": 744685872, + "used": 744685872, + "available": 0 + } + } + }, + "resourceUsage": { + "userCpuSeconds": 18.2241, + "kernelCpuSeconds": 0.986466, + "cpuConsumptionPercent": 91.4789, + "maxRss": 2314416947200, + "pageFaults": { + "IORequired": 24, + "IONotRequired": 552852 + }, + "fsActivity": { + "reads": 0, + "writes": 0 + } + }, + "libuv": [ + ], + "environmentVariables": { + "AUTOJUMP_ERROR_PATH": "/Users/shanshihao/Library/autojump/errors.log", + "rvm_bin_path": "/Users/shanshihao/.rvm/bin", + "NVM_CD_FLAGS": "-q", + "AUTOJUMP_SOURCED": "1", + "ANDROID_HOME": "/Users/shanshihao/Library/Android/sdk", + "VSCODE_NODE_CACHED_DATA_DIR": "/Users/shanshihao/Library/Application Support/Code/CachedData/ff915844119ce9485abfe8aa9076ec76b5300ddd", + "SHELL": "/bin/zsh", + "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", + "TMPDIR": "/var/folders/z1/_7w7404x649flpzp42kcb_qh0000gn/T/", + "Apple_PubSub_Socket_Render": "/private/tmp/com.apple.launchd.zoXrPUqm1J/Render", + "ZSH": "/Users/shanshihao/.oh-my-zsh", + "LC_ALL": "en_US.UTF-8", + "NVM_DIR": "/Users/shanshihao/.nvm", + "USER": "shanshihao", + "_system_type": "Darwin", + "rvm_path": "/Users/shanshihao/.rvm", + "SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.il2DpOaJrd/Listeners", + "__CF_USER_TEXT_ENCODING": "0x1F5:0x19:0x34", + "VSCODE_LOG_STACK": "false", + "PAGER": "less", + "ELECTRON_RUN_AS_NODE": "1", + "LSCOLORS": "Gxfxcxdxbxegedabagacad", + "rvm_prefix": "/Users/shanshihao", + "VSCODE_LOGS": "/Users/shanshihao/Library/Application Support/Code/logs/20200428T095252", + "PATH": "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/shanshihao/.rvm/bin:/Users/shanshihao/Library/Android/sdk/platform-tools", + "_": "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node", + "PWD": "/Users/shanshihao/code/leetcode-javascript", + "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", + "JAVA_HOME": "/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home", + "LANG": "en_US.UTF-8", + "_system_arch": "x86_64", + "XPC_FLAGS": "0x0", + "_system_version": "10.14", + "XPC_SERVICE_NAME": "com.microsoft.VSCode.14628", + "rvm_version": "1.29.3 (latest)", + "SHLVL": "1", + "HOME": "/Users/shanshihao", + "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", + "PIPE_LOGGING": "true", + "VSCODE_IPC_HOOK_EXTHOST": "/var/folders/z1/_7w7404x649flpzp42kcb_qh0000gn/T/vscode-ipc-d29ae846-37d0-49b7-b251-8983a780f2ff.sock", + "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_translationsConfigFile\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn/tcf.json\",\"_cacheRoot\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn/ff915844119ce9485abfe8aa9076ec76b5300ddd\",\"_corruptedFile\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn/corrupted.info\",\"_languagePackSupport\":true}", + "LESS": "-R", + "LOGNAME": "shanshihao", + "LC_CTYPE": "", + "VSCODE_IPC_HOOK": "/Users/shanshihao/Library/Application Support/Code/1.44.2-main.sock", + "NVM_BIN": "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin", + "VSCODE_PID": "51672", + "VERBOSE_LOGGING": "true", + "_system_name": "OSX" + }, + "userLimits": { + "core_file_size_blocks": { + "soft": 0, + "hard": "unlimited" + }, + "data_seg_size_kbytes": { + "soft": "unlimited", + "hard": "unlimited" + }, + "file_size_blocks": { + "soft": "unlimited", + "hard": "unlimited" + }, + "max_locked_memory_bytes": { + "soft": "unlimited", + "hard": "unlimited" + }, + "max_memory_size_kbytes": { + "soft": "unlimited", + "hard": "unlimited" + }, + "open_files": { + "soft": 24576, + "hard": "unlimited" + }, + "stack_size_bytes": { + "soft": 8388608, + "hard": 67104768 + }, + "cpu_time_seconds": { + "soft": "unlimited", + "hard": "unlimited" + }, + "max_user_processes": { + "soft": 1418, + "hard": 2128 + }, + "virtual_memory_kbytes": { + "soft": "unlimited", + "hard": "unlimited" + } + }, + "sharedObjects": [ + "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node", + "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation", + "/usr/lib/libSystem.B.dylib", + "/usr/lib/libc++.1.dylib", + "/usr/lib/libobjc.A.dylib", + "/usr/lib/libDiagnosticMessagesClient.dylib", + "/usr/lib/libicucore.A.dylib", + "/usr/lib/libz.1.dylib", + "/usr/lib/libc++abi.dylib", + "/usr/lib/system/libcache.dylib", + "/usr/lib/system/libcommonCrypto.dylib", + "/usr/lib/system/libcompiler_rt.dylib", + "/usr/lib/system/libcopyfile.dylib", + "/usr/lib/system/libcorecrypto.dylib", + "/usr/lib/system/libdispatch.dylib", + "/usr/lib/system/libdyld.dylib", + "/usr/lib/system/libkeymgr.dylib", + "/usr/lib/system/liblaunch.dylib", + "/usr/lib/system/libmacho.dylib", + "/usr/lib/system/libquarantine.dylib", + "/usr/lib/system/libremovefile.dylib", + "/usr/lib/system/libsystem_asl.dylib", + "/usr/lib/system/libsystem_blocks.dylib", + "/usr/lib/system/libsystem_c.dylib", + "/usr/lib/system/libsystem_configuration.dylib", + "/usr/lib/system/libsystem_coreservices.dylib", + "/usr/lib/system/libsystem_darwin.dylib", + "/usr/lib/system/libsystem_dnssd.dylib", + "/usr/lib/system/libsystem_info.dylib", + "/usr/lib/system/libsystem_m.dylib", + "/usr/lib/system/libsystem_malloc.dylib", + "/usr/lib/system/libsystem_networkextension.dylib", + "/usr/lib/system/libsystem_notify.dylib", + "/usr/lib/system/libsystem_sandbox.dylib", + "/usr/lib/system/libsystem_secinit.dylib", + "/usr/lib/system/libsystem_kernel.dylib", + "/usr/lib/system/libsystem_platform.dylib", + "/usr/lib/system/libsystem_pthread.dylib", + "/usr/lib/system/libsystem_symptoms.dylib", + "/usr/lib/system/libsystem_trace.dylib", + "/usr/lib/system/libunwind.dylib", + "/usr/lib/system/libxpc.dylib", + "/System/Library/CoreServices/Encodings/libSimplifiedChineseConverter.dylib", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices", + "/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics", + "/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO", + "/System/Library/Frameworks/ColorSync.framework/Versions/A/ColorSync", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/ATS", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ColorSyncLegacy.framework/Versions/A/ColorSyncLegacy", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/HIServices", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LangAnalysis.framework/Versions/A/LangAnalysis", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/PrintCore", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/QD.framework/Versions/A/QD", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/SpeechSynthesis.framework/Versions/A/SpeechSynthesis", + "/System/Library/PrivateFrameworks/SkyLight.framework/Versions/A/SkyLight", + "/System/Library/Frameworks/IOSurface.framework/Versions/A/IOSurface", + "/usr/lib/libxml2.2.dylib", + "/System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate", + "/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation", + "/usr/lib/libcompression.dylib", + "/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration", + "/System/Library/Frameworks/CoreDisplay.framework/Versions/A/CoreDisplay", + "/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit", + "/System/Library/Frameworks/Metal.framework/Versions/A/Metal", + "/System/Library/Frameworks/MetalPerformanceShaders.framework/Versions/A/MetalPerformanceShaders", + "/System/Library/PrivateFrameworks/MultitouchSupport.framework/Versions/A/MultitouchSupport", + "/System/Library/Frameworks/Security.framework/Versions/A/Security", + "/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore", + "/usr/lib/libbsm.0.dylib", + "/usr/lib/liblzma.5.dylib", + "/usr/lib/libauto.dylib", + "/System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration", + "/usr/lib/libarchive.2.dylib", + "/usr/lib/liblangid.dylib", + "/usr/lib/libCRFSuite.dylib", + "/usr/lib/libenergytrace.dylib", + "/usr/lib/system/libkxld.dylib", + "/System/Library/PrivateFrameworks/AppleFSCompression.framework/Versions/A/AppleFSCompression", + "/usr/lib/libOpenScriptingUtil.dylib", + "/usr/lib/libcoretls.dylib", + "/usr/lib/libcoretls_cfhelpers.dylib", + "/usr/lib/libpam.2.dylib", + "/usr/lib/libsqlite3.dylib", + "/usr/lib/libxar.1.dylib", + "/usr/lib/libbz2.1.0.dylib", + "/usr/lib/libnetwork.dylib", + "/usr/lib/libapple_nghttp2.dylib", + "/usr/lib/libpcap.A.dylib", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/FSEvents", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/DictionaryServices.framework/Versions/A/DictionaryServices", + "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SharedFileList.framework/Versions/A/SharedFileList", + "/System/Library/Frameworks/NetFS.framework/Versions/A/NetFS", + "/System/Library/PrivateFrameworks/NetAuth.framework/Versions/A/NetAuth", + "/System/Library/PrivateFrameworks/login.framework/Versions/A/Frameworks/loginsupport.framework/Versions/A/loginsupport", + "/System/Library/PrivateFrameworks/TCC.framework/Versions/A/TCC", + "/System/Library/PrivateFrameworks/CoreNLP.framework/Versions/A/CoreNLP", + "/System/Library/PrivateFrameworks/MetadataUtilities.framework/Versions/A/MetadataUtilities", + "/usr/lib/libmecabra.dylib", + "/usr/lib/libmecab.1.0.0.dylib", + "/usr/lib/libgermantok.dylib", + "/usr/lib/libThaiTokenizer.dylib", + "/usr/lib/libChineseTokenizer.dylib", + "/usr/lib/libiconv.2.dylib", + "/usr/lib/libcharset.1.dylib", + "/System/Library/PrivateFrameworks/LanguageModeling.framework/Versions/A/LanguageModeling", + "/System/Library/PrivateFrameworks/CoreEmoji.framework/Versions/A/CoreEmoji", + "/System/Library/PrivateFrameworks/Lexicon.framework/Versions/A/Lexicon", + "/System/Library/PrivateFrameworks/LinguisticData.framework/Versions/A/LinguisticData", + "/usr/lib/libcmph.dylib", + "/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData", + "/System/Library/Frameworks/OpenDirectory.framework/Versions/A/Frameworks/CFOpenDirectory.framework/Versions/A/CFOpenDirectory", + "/System/Library/PrivateFrameworks/APFS.framework/Versions/A/APFS", + "/usr/lib/libutil.dylib", + "/System/Library/Frameworks/ServiceManagement.framework/Versions/A/ServiceManagement", + "/System/Library/PrivateFrameworks/BackgroundTaskManagement.framework/Versions/A/BackgroundTaskManagement", + "/usr/lib/libxslt.1.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vImage.framework/Versions/A/vImage", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/vecLib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvMisc.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvDSP.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLinearAlgebra.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparseBLAS.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libQuadrature.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBNNS.dylib", + "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparse.dylib", + "/System/Library/PrivateFrameworks/GPUWrangler.framework/Versions/A/GPUWrangler", + "/System/Library/PrivateFrameworks/IOAccelerator.framework/Versions/A/IOAccelerator", + "/System/Library/PrivateFrameworks/IOPresentment.framework/Versions/A/IOPresentment", + "/System/Library/PrivateFrameworks/DSExternalDisplay.framework/Versions/A/DSExternalDisplay", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCoreFSCache.dylib", + "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSCore.framework/Versions/A/MPSCore", + "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSImage.framework/Versions/A/MPSImage", + "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSNeuralNetwork.framework/Versions/A/MPSNeuralNetwork", + "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSMatrix.framework/Versions/A/MPSMatrix", + "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSRayIntersector.framework/Versions/A/MPSRayIntersector", + "/System/Library/PrivateFrameworks/MetalTools.framework/Versions/A/MetalTools", + "/System/Library/PrivateFrameworks/AggregateDictionary.framework/Versions/A/AggregateDictionary", + "/usr/lib/libMobileGestalt.dylib", + "/System/Library/Frameworks/CoreImage.framework/Versions/A/CoreImage", + "/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL", + "/System/Library/PrivateFrameworks/GraphVisualizer.framework/Versions/A/GraphVisualizer", + "/System/Library/PrivateFrameworks/FaceCore.framework/Versions/A/FaceCore", + "/System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL", + "/usr/lib/libFosl_dynamic.dylib", + "/System/Library/PrivateFrameworks/OTSVG.framework/Versions/A/OTSVG", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontParser.dylib", + "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontRegistry.dylib", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libPng.dylib", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libGIF.dylib", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJP2.dylib", + "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libRadiance.dylib", + "/System/Library/PrivateFrameworks/AppleJPEG.framework/Versions/A/AppleJPEG", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGFXShared.dylib", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLImage.dylib", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCVMSPluginSupport.dylib", + "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCoreVMClient.dylib", + "/usr/lib/libcups.2.dylib", + "/System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos", + "/System/Library/Frameworks/GSS.framework/Versions/A/GSS", + "/usr/lib/libresolv.9.dylib", + "/System/Library/PrivateFrameworks/Heimdal.framework/Versions/A/Heimdal", + "/usr/lib/libheimdal-asn1.dylib", + "/System/Library/Frameworks/OpenDirectory.framework/Versions/A/OpenDirectory", + "/System/Library/PrivateFrameworks/CommonAuth.framework/Versions/A/CommonAuth", + "/System/Library/Frameworks/SecurityFoundation.framework/Versions/A/SecurityFoundation", + "/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio", + "/System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox", + "/System/Library/PrivateFrameworks/AppleSauce.framework/Versions/A/AppleSauce", + "/System/Library/PrivateFrameworks/AssertionServices.framework/Versions/A/AssertionServices", + "/System/Library/PrivateFrameworks/BaseBoard.framework/Versions/A/BaseBoard" + ] +} \ No newline at end of file diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" index 95eeee0..b7f5fe0 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" @@ -3,50 +3,49 @@ * @return {boolean} */ let canPartition = function (nums) { + let n = nums.length + let sum = nums.reduce((a, b) => a + b); let target = sum / 2; + + // 数据不是整数 直接return if (Math.ceil(target) !== target) { return false; } - let n = nums.length; let dp = new Array(n); for (let i = 0; i < dp.length; i++) { dp[i] = new Array(target + 1).fill(false); } - for (let j = 0; j <= target; j++) { - dp[0][j] = nums[0] === target ? true : false; - } - + // 列代表可以选择去凑数的数值 for (let i = 0; i < dp.length; i++) { - dp[i][0] = true; - } - - for (let i = 1; i < dp.length; i++) { + // 行代表是否可以凑到这个数字j for (let j = 0; j <= target; j++) { - let num = nums[i]; + // 不用当前数,直接选择前一行的结果 + let pickPrev = (dp[i - 1] ? dp[i - 1][j]: false) || false + + // 拿出当前数,并且从前一行里找其他的值能否凑成剩下的值 + let pickCurrentAndPrev = (dp[i - 1] ? dp[i - 1][j - nums[i]]: false) || false - let pick = dp[i - 1][j - num] || false; - let notPick = dp[i - 1][j] || false; + // 只拿的值直接去凑目标值 + let pickCurrent = j === nums[i] - let result = pick || notPick; - dp[i][j] = result; + // 任意一者满足 即可理解成 「i下标的值」配合「i下标之前的数值」 可以一起凑成目标值 + let can = ( + pickPrev || + pickCurrent|| + pickCurrentAndPrev + ) - if (j == target && result) { - return true; + dp[i][j] = can + + // 只要任意一行的 target 列满足条件 即可认为有「子数组」可以凑成目标值 直接返回 true + if ((j === target) && can) { + return true } } } - - return dp[n - 1][target]; -}; - -console.log(canPartition([1, 2, 3, 5])); - -/** - * 这个问题的思路在于,先把数组之和除以二,记为target。只要任意组合的子数组能凑成target,也就说明剩下的一定也能凑成target。 - * 1. 除以二后有小数点的直接失败,因为一定不可能是两个整数子数组相凑的结果。 - * 2. 只要用任意数量的子数组可以拼凑出来target的值,也就是dp数组的任意一层的最右边的值计算出是true,那么整题的结果就为true。因为不论你用几个值凑出了target值,哪怕只用了一个值。另外剩下的值之和一定也是target。 - */ + return dp[n - 1][target] +}; \ No newline at end of file diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" index 626c8bd..c4f56f9 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227-509.js" @@ -3,18 +3,31 @@ * @return {number} */ let fib = function (N) { - let dp = [] - - dp[0] = 0n; - dp[1] = 1n; + let prev = 1n + let prevPrev = 1n for (let i = 2; i <= N; i++) { - dp[i] = dp[i - 1] + dp[i - 2]; + let current = prev + prevPrev + prevPrev = prev + prev = current } - return dp[N]; + return prev }; +// let fib = function (N) { +// let dp = [] + +// dp[0] = 0n; +// dp[1] = 1n; + +// for (let i = 2; i <= N; i++) { +// dp[i] = dp[i - 1] + dp[i - 2]; +// } + +// return dp[N]; +// }; + // let fib = (function () { // let memo = new Map(); // return function (n) { From a2cd6f64b0ba91bd4a8dadcb6a8f86d46ba1861e Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 7 May 2020 22:23:43 +0800 Subject: [PATCH 045/145] =?UTF-8?q?feat:=20=E5=87=91=E7=A1=AC=E5=B8=81=20?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\242\23008.11.\347\241\254\345\270\201.js" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" new file mode 100644 index 0000000..d5bf022 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" @@ -0,0 +1,52 @@ +let coins = [1, 5, 10, 25]; +let waysToChange = function (n) { + let cl = coins.length; + if (n === 0) return 0; + + let dp = new Array(cl); + for (let i = 0; i < cl; i++) { + dp[i] = new Array(n + 1); + } + + for (let i = 0; i < dp.length; i++) { + for (let j = 1; j <= n; j++) { + let coin = coins[i]; + let dpPrev = dp[i - 1]; + // 考虑不用当前硬币 + let pickPrev = dpPrev ? dpPrev[j] : 0; + + // 考虑加上当前硬币 + let pickCurrentAndPrev = 0; + if (j > coin && dpPrev) { + // 用了当前的硬币以后 剩余的面值 + let duplicate = 1 + while (coin * duplicate < j) { + let rest = j - coin * duplicate + let pickRest = dpPrev[rest]; + if (pickRest > 0) { + // 这个方式数其实就是凑剩余硬币的方式数 + // 比如 以硬币5和面值10来说 + // 拿出了5 发现剩余面值是5 + // 凑剩余面值5的情况是 5 + 11111 + // 所以拿出5来凑的方式是1种 + pickCurrentAndPrev += pickRest; + } + + duplicate ++ + } + } + + let pickOnlyCurrnet = 0; + if (j >= coin) { + // 除后没有余数 说明是可以只用当前硬币的 + // 比如用5凑5 5+5来凑10 用5+5+5凑15 + pickOnlyCurrnet = j % coin === 0 ? 1 : 0; + } + + dp[i][j] = (pickPrev + pickCurrentAndPrev + pickOnlyCurrnet) % 1000000007; + } + } + return dp[cl - 1][n]; +}; + +console.log(waysToChange(61)) // 73 \ No newline at end of file From 2eb2ce3c8b3eb8667327705ebf792d4c56ed7e83 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 8 May 2020 01:51:46 +0800 Subject: [PATCH 046/145] =?UTF-8?q?fix:=20=E5=87=91=E7=A1=AC=E5=B8=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\242\23008.11.\347\241\254\345\270\201.js" | 41 ++++++++----------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" index d5bf022..6b29456 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\235\242\350\257\225\351\242\23008.11.\347\241\254\345\270\201.js" @@ -8,6 +8,10 @@ let waysToChange = function (n) { dp[i] = new Array(n + 1); } + for (let i = 0; i < cl; i++) { + dp[i][0] = 1 + } + for (let i = 0; i < dp.length; i++) { for (let j = 1; j <= n; j++) { let coin = coins[i]; @@ -17,36 +21,25 @@ let waysToChange = function (n) { // 考虑加上当前硬币 let pickCurrentAndPrev = 0; - if (j > coin && dpPrev) { + if (j >= coin) { // 用了当前的硬币以后 剩余的面值 - let duplicate = 1 - while (coin * duplicate < j) { - let rest = j - coin * duplicate - let pickRest = dpPrev[rest]; - if (pickRest > 0) { - // 这个方式数其实就是凑剩余硬币的方式数 - // 比如 以硬币5和面值10来说 - // 拿出了5 发现剩余面值是5 - // 凑剩余面值5的情况是 5 + 11111 - // 所以拿出5来凑的方式是1种 - pickCurrentAndPrev += pickRest; - } - - duplicate ++ + let rest = j - coin; + let pickRest = dp[i][rest]; + if (pickRest > 0) { + // 这个方式数其实就是凑剩余硬币的方式数 + // 比如 以硬币5和面值10来说 + // 拿出了5 发现剩余面值是5 + // 凑剩余面值5的情况是 5 + 11111 + // 所以拿出5来凑的方式是1种 + pickCurrentAndPrev = pickRest; } } - let pickOnlyCurrnet = 0; - if (j >= coin) { - // 除后没有余数 说明是可以只用当前硬币的 - // 比如用5凑5 5+5来凑10 用5+5+5凑15 - pickOnlyCurrnet = j % coin === 0 ? 1 : 0; - } - - dp[i][j] = (pickPrev + pickCurrentAndPrev + pickOnlyCurrnet) % 1000000007; + dp[i][j] = (pickPrev + pickCurrentAndPrev) % 1000000007; } } + return dp[cl - 1][n]; }; -console.log(waysToChange(61)) // 73 \ No newline at end of file +console.log(waysToChange(61)); // 73 From 67ee5854cb24da53eac4496f9c2fff4c145199f6 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 8 May 2020 11:08:50 +0800 Subject: [PATCH 047/145] fix: remove report --- report.20200507.190200.31578.0.001.json | 584 ------------------------ 1 file changed, 584 deletions(-) delete mode 100644 report.20200507.190200.31578.0.001.json diff --git a/report.20200507.190200.31578.0.001.json b/report.20200507.190200.31578.0.001.json deleted file mode 100644 index 83e1ea5..0000000 --- a/report.20200507.190200.31578.0.001.json +++ /dev/null @@ -1,584 +0,0 @@ - -{ - "header": { - "reportVersion": 1, - "event": "Allocation failed - JavaScript heap out of memory", - "trigger": "FatalError", - "filename": "report.20200507.190200.31578.0.001.json", - "dumpEventTime": "2020-05-07T19:02:00Z", - "dumpEventTimeStamp": "1588849320349", - "processId": 31578, - "cwd": "/Users/shanshihao/code/leetcode-javascript", - "commandLine": [ - "node", - "/Users/shanshihao/code/leetcode-javascript/动态规划/分割等和子集-416.js" - ], - "nodejsVersion": "v12.14.1", - "wordSize": 64, - "arch": "x64", - "platform": "darwin", - "componentVersions": { - "node": "12.14.1", - "v8": "7.7.299.13-node.16", - "uv": "1.33.1", - "zlib": "1.2.11", - "brotli": "1.0.7", - "ares": "1.15.0", - "modules": "72", - "nghttp2": "1.40.0", - "napi": "5", - "llhttp": "2.0.1", - "http_parser": "2.8.0", - "openssl": "1.1.1d", - "cldr": "35.1", - "icu": "64.2", - "tz": "2019c", - "unicode": "12.1" - }, - "release": { - "name": "node", - "lts": "Erbium", - "headersUrl": "https://nodejs.org/download/release/v12.14.1/node-v12.14.1-headers.tar.gz", - "sourceUrl": "https://nodejs.org/download/release/v12.14.1/node-v12.14.1.tar.gz" - }, - "osName": "Darwin", - "osRelease": "18.5.0", - "osVersion": "Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64", - "osMachine": "x86_64", - "cpus": [ - { - "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", - "speed": 2300, - "user": 212947840, - "nice": 0, - "sys": 149762140, - "idle": 576349080, - "irq": 0 - }, - { - "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", - "speed": 2300, - "user": 148908440, - "nice": 0, - "sys": 77507840, - "idle": 712452200, - "irq": 0 - }, - { - "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", - "speed": 2300, - "user": 214894110, - "nice": 0, - "sys": 132663920, - "idle": 591310940, - "irq": 0 - }, - { - "model": "Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz", - "speed": 2300, - "user": 132767920, - "nice": 0, - "sys": 63910290, - "idle": 742190080, - "irq": 0 - } - ], - "networkInterfaces": [ - { - "name": "lo0", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "127.0.0.1", - "netmask": "255.0.0.0", - "family": "IPv4" - }, - { - "name": "lo0", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "::1", - "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - "family": "IPv6", - "scopeid": 0 - }, - { - "name": "lo0", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "fe80::1", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 1 - }, - { - "name": "en0", - "internal": false, - "mac": "88:e9:fe:50:9c:d2", - "address": "fe80::18b9:7632:9a0e:831e", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 6 - }, - { - "name": "en0", - "internal": false, - "mac": "88:e9:fe:50:9c:d2", - "address": "30.43.84.43", - "netmask": "255.255.240.0", - "family": "IPv4" - }, - { - "name": "awdl0", - "internal": false, - "mac": "6e:5a:0e:89:b0:73", - "address": "fe80::6c5a:eff:fe89:b073", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 8 - }, - { - "name": "utun0", - "internal": false, - "mac": "00:00:00:00:00:00", - "address": "fe80::17be:efd0:e58e:24c2", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 12 - }, - { - "name": "utun1", - "internal": false, - "mac": "00:00:00:00:00:00", - "address": "fe80::847c:7463:1209:7935", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 13 - }, - { - "name": "utun2", - "internal": false, - "mac": "00:00:00:00:00:00", - "address": "fe80::cc27:fa71:3472:6bed", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 14 - }, - { - "name": "utun3", - "internal": false, - "mac": "00:00:00:00:00:00", - "address": "fe80::f4f:d641:8cbd:6df3", - "netmask": "ffff:ffff:ffff:ffff::", - "family": "IPv6", - "scopeid": 15 - } - ], - "host": "shanshihaodeMacBook-Pro.local" - }, - "javascriptStack": { - "message": "No stack.", - "stack": [ - "Unavailable." - ] - }, - "nativeStack": [ - { - "pc": "0x000000010014e0ce", - "symbol": "report::TriggerNodeReport(v8::Isolate*, node::Environment*, char const*, char const*, std::__1::basic_string, std::__1::allocator > const&, v8::Local) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x000000010007f391", - "symbol": "node::OnFatalError(char const*, char const*) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x0000000100176887", - "symbol": "v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x0000000100176823", - "symbol": "v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x00000001002fa9d5", - "symbol": "v8::internal::Heap::FatalProcessOutOfMemory(char const*) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x00000001002ce0b1", - "symbol": "v8::internal::Factory::NewFixedArrayWithFiller(v8::internal::RootIndex, int, v8::internal::Object, v8::internal::AllocationType) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x0000000100439d5a", - "symbol": "v8::internal::(anonymous namespace)::ElementsAccessorBase >::GrowCapacity(v8::internal::Handle, unsigned int) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x00000001005d2f04", - "symbol": "v8::internal::Runtime_GrowArrayElements(int, unsigned long*, v8::internal::Isolate*) [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - }, - { - "pc": "0x00000001009311f9", - "symbol": "Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node]" - } - ], - "javascriptHeap": { - "totalMemory": 2269442048, - "totalCommittedMemory": 2237021920, - "usedMemory": 2234924776, - "availableMemory": 17192408, - "memoryLimit": 2197815296, - "heapSpaces": { - "read_only_space": { - "memorySize": 262144, - "committedMemory": 32568, - "capacity": 261872, - "used": 32296, - "available": 229576 - }, - "new_space": { - "memorySize": 33554432, - "committedMemory": 1981616, - "capacity": 16759808, - "used": 0, - "available": 16759808 - }, - "old_space": { - "memorySize": 1945600, - "committedMemory": 1594216, - "capacity": 1795064, - "used": 1592040, - "available": 203024 - }, - "code_space": { - "memorySize": 425984, - "committedMemory": 176128, - "capacity": 159744, - "used": 159744, - "available": 0 - }, - "map_space": { - "memorySize": 266240, - "committedMemory": 249744, - "capacity": 249200, - "used": 249200, - "available": 0 - }, - "large_object_space": { - "memorySize": 1488248832, - "committedMemory": 1488248832, - "capacity": 1488202072, - "used": 1488202072, - "available": 0 - }, - "code_large_object_space": { - "memorySize": 49152, - "committedMemory": 49152, - "capacity": 3552, - "used": 3552, - "available": 0 - }, - "new_large_object_space": { - "memorySize": 744689664, - "committedMemory": 744689664, - "capacity": 744685872, - "used": 744685872, - "available": 0 - } - } - }, - "resourceUsage": { - "userCpuSeconds": 18.2241, - "kernelCpuSeconds": 0.986466, - "cpuConsumptionPercent": 91.4789, - "maxRss": 2314416947200, - "pageFaults": { - "IORequired": 24, - "IONotRequired": 552852 - }, - "fsActivity": { - "reads": 0, - "writes": 0 - } - }, - "libuv": [ - ], - "environmentVariables": { - "AUTOJUMP_ERROR_PATH": "/Users/shanshihao/Library/autojump/errors.log", - "rvm_bin_path": "/Users/shanshihao/.rvm/bin", - "NVM_CD_FLAGS": "-q", - "AUTOJUMP_SOURCED": "1", - "ANDROID_HOME": "/Users/shanshihao/Library/Android/sdk", - "VSCODE_NODE_CACHED_DATA_DIR": "/Users/shanshihao/Library/Application Support/Code/CachedData/ff915844119ce9485abfe8aa9076ec76b5300ddd", - "SHELL": "/bin/zsh", - "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", - "TMPDIR": "/var/folders/z1/_7w7404x649flpzp42kcb_qh0000gn/T/", - "Apple_PubSub_Socket_Render": "/private/tmp/com.apple.launchd.zoXrPUqm1J/Render", - "ZSH": "/Users/shanshihao/.oh-my-zsh", - "LC_ALL": "en_US.UTF-8", - "NVM_DIR": "/Users/shanshihao/.nvm", - "USER": "shanshihao", - "_system_type": "Darwin", - "rvm_path": "/Users/shanshihao/.rvm", - "SSH_AUTH_SOCK": "/private/tmp/com.apple.launchd.il2DpOaJrd/Listeners", - "__CF_USER_TEXT_ENCODING": "0x1F5:0x19:0x34", - "VSCODE_LOG_STACK": "false", - "PAGER": "less", - "ELECTRON_RUN_AS_NODE": "1", - "LSCOLORS": "Gxfxcxdxbxegedabagacad", - "rvm_prefix": "/Users/shanshihao", - "VSCODE_LOGS": "/Users/shanshihao/Library/Application Support/Code/logs/20200428T095252", - "PATH": "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/shanshihao/.rvm/bin:/Users/shanshihao/Library/Android/sdk/platform-tools", - "_": "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node", - "PWD": "/Users/shanshihao/code/leetcode-javascript", - "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", - "JAVA_HOME": "/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home", - "LANG": "en_US.UTF-8", - "_system_arch": "x86_64", - "XPC_FLAGS": "0x0", - "_system_version": "10.14", - "XPC_SERVICE_NAME": "com.microsoft.VSCode.14628", - "rvm_version": "1.29.3 (latest)", - "SHLVL": "1", - "HOME": "/Users/shanshihao", - "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", - "PIPE_LOGGING": "true", - "VSCODE_IPC_HOOK_EXTHOST": "/var/folders/z1/_7w7404x649flpzp42kcb_qh0000gn/T/vscode-ipc-d29ae846-37d0-49b7-b251-8983a780f2ff.sock", - "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_translationsConfigFile\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn/tcf.json\",\"_cacheRoot\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn/ff915844119ce9485abfe8aa9076ec76b5300ddd\",\"_corruptedFile\":\"/Users/shanshihao/Library/Application Support/Code/clp/bde907400d9a4fa1269f7dbaf14dc5b1.zh-cn/corrupted.info\",\"_languagePackSupport\":true}", - "LESS": "-R", - "LOGNAME": "shanshihao", - "LC_CTYPE": "", - "VSCODE_IPC_HOOK": "/Users/shanshihao/Library/Application Support/Code/1.44.2-main.sock", - "NVM_BIN": "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin", - "VSCODE_PID": "51672", - "VERBOSE_LOGGING": "true", - "_system_name": "OSX" - }, - "userLimits": { - "core_file_size_blocks": { - "soft": 0, - "hard": "unlimited" - }, - "data_seg_size_kbytes": { - "soft": "unlimited", - "hard": "unlimited" - }, - "file_size_blocks": { - "soft": "unlimited", - "hard": "unlimited" - }, - "max_locked_memory_bytes": { - "soft": "unlimited", - "hard": "unlimited" - }, - "max_memory_size_kbytes": { - "soft": "unlimited", - "hard": "unlimited" - }, - "open_files": { - "soft": 24576, - "hard": "unlimited" - }, - "stack_size_bytes": { - "soft": 8388608, - "hard": 67104768 - }, - "cpu_time_seconds": { - "soft": "unlimited", - "hard": "unlimited" - }, - "max_user_processes": { - "soft": 1418, - "hard": 2128 - }, - "virtual_memory_kbytes": { - "soft": "unlimited", - "hard": "unlimited" - } - }, - "sharedObjects": [ - "/Users/shanshihao/.nvm/versions/node/v12.14.1/bin/node", - "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation", - "/usr/lib/libSystem.B.dylib", - "/usr/lib/libc++.1.dylib", - "/usr/lib/libobjc.A.dylib", - "/usr/lib/libDiagnosticMessagesClient.dylib", - "/usr/lib/libicucore.A.dylib", - "/usr/lib/libz.1.dylib", - "/usr/lib/libc++abi.dylib", - "/usr/lib/system/libcache.dylib", - "/usr/lib/system/libcommonCrypto.dylib", - "/usr/lib/system/libcompiler_rt.dylib", - "/usr/lib/system/libcopyfile.dylib", - "/usr/lib/system/libcorecrypto.dylib", - "/usr/lib/system/libdispatch.dylib", - "/usr/lib/system/libdyld.dylib", - "/usr/lib/system/libkeymgr.dylib", - "/usr/lib/system/liblaunch.dylib", - "/usr/lib/system/libmacho.dylib", - "/usr/lib/system/libquarantine.dylib", - "/usr/lib/system/libremovefile.dylib", - "/usr/lib/system/libsystem_asl.dylib", - "/usr/lib/system/libsystem_blocks.dylib", - "/usr/lib/system/libsystem_c.dylib", - "/usr/lib/system/libsystem_configuration.dylib", - "/usr/lib/system/libsystem_coreservices.dylib", - "/usr/lib/system/libsystem_darwin.dylib", - "/usr/lib/system/libsystem_dnssd.dylib", - "/usr/lib/system/libsystem_info.dylib", - "/usr/lib/system/libsystem_m.dylib", - "/usr/lib/system/libsystem_malloc.dylib", - "/usr/lib/system/libsystem_networkextension.dylib", - "/usr/lib/system/libsystem_notify.dylib", - "/usr/lib/system/libsystem_sandbox.dylib", - "/usr/lib/system/libsystem_secinit.dylib", - "/usr/lib/system/libsystem_kernel.dylib", - "/usr/lib/system/libsystem_platform.dylib", - "/usr/lib/system/libsystem_pthread.dylib", - "/usr/lib/system/libsystem_symptoms.dylib", - "/usr/lib/system/libsystem_trace.dylib", - "/usr/lib/system/libunwind.dylib", - "/usr/lib/system/libxpc.dylib", - "/System/Library/CoreServices/Encodings/libSimplifiedChineseConverter.dylib", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices", - "/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics", - "/System/Library/Frameworks/CoreText.framework/Versions/A/CoreText", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO", - "/System/Library/Frameworks/ColorSync.framework/Versions/A/ColorSync", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/ATS", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ColorSyncLegacy.framework/Versions/A/ColorSyncLegacy", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/HIServices.framework/Versions/A/HIServices", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LangAnalysis.framework/Versions/A/LangAnalysis", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/PrintCore", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/QD.framework/Versions/A/QD", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/SpeechSynthesis.framework/Versions/A/SpeechSynthesis", - "/System/Library/PrivateFrameworks/SkyLight.framework/Versions/A/SkyLight", - "/System/Library/Frameworks/IOSurface.framework/Versions/A/IOSurface", - "/usr/lib/libxml2.2.dylib", - "/System/Library/Frameworks/CFNetwork.framework/Versions/A/CFNetwork", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate", - "/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation", - "/usr/lib/libcompression.dylib", - "/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration", - "/System/Library/Frameworks/CoreDisplay.framework/Versions/A/CoreDisplay", - "/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit", - "/System/Library/Frameworks/Metal.framework/Versions/A/Metal", - "/System/Library/Frameworks/MetalPerformanceShaders.framework/Versions/A/MetalPerformanceShaders", - "/System/Library/PrivateFrameworks/MultitouchSupport.framework/Versions/A/MultitouchSupport", - "/System/Library/Frameworks/Security.framework/Versions/A/Security", - "/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore", - "/usr/lib/libbsm.0.dylib", - "/usr/lib/liblzma.5.dylib", - "/usr/lib/libauto.dylib", - "/System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration", - "/usr/lib/libarchive.2.dylib", - "/usr/lib/liblangid.dylib", - "/usr/lib/libCRFSuite.dylib", - "/usr/lib/libenergytrace.dylib", - "/usr/lib/system/libkxld.dylib", - "/System/Library/PrivateFrameworks/AppleFSCompression.framework/Versions/A/AppleFSCompression", - "/usr/lib/libOpenScriptingUtil.dylib", - "/usr/lib/libcoretls.dylib", - "/usr/lib/libcoretls_cfhelpers.dylib", - "/usr/lib/libpam.2.dylib", - "/usr/lib/libsqlite3.dylib", - "/usr/lib/libxar.1.dylib", - "/usr/lib/libbz2.1.0.dylib", - "/usr/lib/libnetwork.dylib", - "/usr/lib/libapple_nghttp2.dylib", - "/usr/lib/libpcap.A.dylib", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/FSEvents", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/DictionaryServices.framework/Versions/A/DictionaryServices", - "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SharedFileList.framework/Versions/A/SharedFileList", - "/System/Library/Frameworks/NetFS.framework/Versions/A/NetFS", - "/System/Library/PrivateFrameworks/NetAuth.framework/Versions/A/NetAuth", - "/System/Library/PrivateFrameworks/login.framework/Versions/A/Frameworks/loginsupport.framework/Versions/A/loginsupport", - "/System/Library/PrivateFrameworks/TCC.framework/Versions/A/TCC", - "/System/Library/PrivateFrameworks/CoreNLP.framework/Versions/A/CoreNLP", - "/System/Library/PrivateFrameworks/MetadataUtilities.framework/Versions/A/MetadataUtilities", - "/usr/lib/libmecabra.dylib", - "/usr/lib/libmecab.1.0.0.dylib", - "/usr/lib/libgermantok.dylib", - "/usr/lib/libThaiTokenizer.dylib", - "/usr/lib/libChineseTokenizer.dylib", - "/usr/lib/libiconv.2.dylib", - "/usr/lib/libcharset.1.dylib", - "/System/Library/PrivateFrameworks/LanguageModeling.framework/Versions/A/LanguageModeling", - "/System/Library/PrivateFrameworks/CoreEmoji.framework/Versions/A/CoreEmoji", - "/System/Library/PrivateFrameworks/Lexicon.framework/Versions/A/Lexicon", - "/System/Library/PrivateFrameworks/LinguisticData.framework/Versions/A/LinguisticData", - "/usr/lib/libcmph.dylib", - "/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData", - "/System/Library/Frameworks/OpenDirectory.framework/Versions/A/Frameworks/CFOpenDirectory.framework/Versions/A/CFOpenDirectory", - "/System/Library/PrivateFrameworks/APFS.framework/Versions/A/APFS", - "/usr/lib/libutil.dylib", - "/System/Library/Frameworks/ServiceManagement.framework/Versions/A/ServiceManagement", - "/System/Library/PrivateFrameworks/BackgroundTaskManagement.framework/Versions/A/BackgroundTaskManagement", - "/usr/lib/libxslt.1.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vImage.framework/Versions/A/vImage", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/vecLib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvMisc.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libvDSP.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLinearAlgebra.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparseBLAS.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libQuadrature.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBNNS.dylib", - "/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libSparse.dylib", - "/System/Library/PrivateFrameworks/GPUWrangler.framework/Versions/A/GPUWrangler", - "/System/Library/PrivateFrameworks/IOAccelerator.framework/Versions/A/IOAccelerator", - "/System/Library/PrivateFrameworks/IOPresentment.framework/Versions/A/IOPresentment", - "/System/Library/PrivateFrameworks/DSExternalDisplay.framework/Versions/A/DSExternalDisplay", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCoreFSCache.dylib", - "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSCore.framework/Versions/A/MPSCore", - "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSImage.framework/Versions/A/MPSImage", - "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSNeuralNetwork.framework/Versions/A/MPSNeuralNetwork", - "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSMatrix.framework/Versions/A/MPSMatrix", - "/System/Library/Frameworks/MetalPerformanceShaders.framework/Frameworks/MPSRayIntersector.framework/Versions/A/MPSRayIntersector", - "/System/Library/PrivateFrameworks/MetalTools.framework/Versions/A/MetalTools", - "/System/Library/PrivateFrameworks/AggregateDictionary.framework/Versions/A/AggregateDictionary", - "/usr/lib/libMobileGestalt.dylib", - "/System/Library/Frameworks/CoreImage.framework/Versions/A/CoreImage", - "/System/Library/Frameworks/CoreVideo.framework/Versions/A/CoreVideo", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL", - "/System/Library/PrivateFrameworks/GraphVisualizer.framework/Versions/A/GraphVisualizer", - "/System/Library/PrivateFrameworks/FaceCore.framework/Versions/A/FaceCore", - "/System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL", - "/usr/lib/libFosl_dynamic.dylib", - "/System/Library/PrivateFrameworks/OTSVG.framework/Versions/A/OTSVG", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontParser.dylib", - "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Resources/libFontRegistry.dylib", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libPng.dylib", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libGIF.dylib", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJP2.dylib", - "/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libRadiance.dylib", - "/System/Library/PrivateFrameworks/AppleJPEG.framework/Versions/A/AppleJPEG", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGFXShared.dylib", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLImage.dylib", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCVMSPluginSupport.dylib", - "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libCoreVMClient.dylib", - "/usr/lib/libcups.2.dylib", - "/System/Library/Frameworks/Kerberos.framework/Versions/A/Kerberos", - "/System/Library/Frameworks/GSS.framework/Versions/A/GSS", - "/usr/lib/libresolv.9.dylib", - "/System/Library/PrivateFrameworks/Heimdal.framework/Versions/A/Heimdal", - "/usr/lib/libheimdal-asn1.dylib", - "/System/Library/Frameworks/OpenDirectory.framework/Versions/A/OpenDirectory", - "/System/Library/PrivateFrameworks/CommonAuth.framework/Versions/A/CommonAuth", - "/System/Library/Frameworks/SecurityFoundation.framework/Versions/A/SecurityFoundation", - "/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio", - "/System/Library/Frameworks/AudioToolbox.framework/Versions/A/AudioToolbox", - "/System/Library/PrivateFrameworks/AppleSauce.framework/Versions/A/AppleSauce", - "/System/Library/PrivateFrameworks/AssertionServices.framework/Versions/A/AssertionServices", - "/System/Library/PrivateFrameworks/BaseBoard.framework/Versions/A/BaseBoard" - ] -} \ No newline at end of file From 44cf1c94cf6282346f626a13b5f73f0eefc3b2c9 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 10 May 2020 00:25:39 +0800 Subject: [PATCH 048/145] =?UTF-8?q?feat:=20x=E7=9A=84=E5=B9=B3=E6=96=B9?= =?UTF-8?q?=E6=A0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\271\263\346\226\271\346\240\271-69.js" | 25 +++++++++++++++++++ ...14\345\210\206\346\237\245\346\211\276.js" | 0 2 files changed, 25 insertions(+) create mode 100644 "\344\272\214\345\210\206\346\237\245\346\211\276/x \347\232\204\345\271\263\346\226\271\346\240\271-69.js" rename "\344\272\214\345\210\206\346\237\245\346\211\276.js" => "\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" (100%) diff --git "a/\344\272\214\345\210\206\346\237\245\346\211\276/x \347\232\204\345\271\263\346\226\271\346\240\271-69.js" "b/\344\272\214\345\210\206\346\237\245\346\211\276/x \347\232\204\345\271\263\346\226\271\346\240\271-69.js" new file mode 100644 index 0000000..7ca5020 --- /dev/null +++ "b/\344\272\214\345\210\206\346\237\245\346\211\276/x \347\232\204\345\271\263\346\226\271\346\240\271-69.js" @@ -0,0 +1,25 @@ +/** + * @param {number} x + * @return {number} + */ +let mySqrt = function (x) { + let left = 0; + let right = x; + let ans = -1 + while (left <= right) { + let mid = Math.round((left + right) / 2); + let product = mid * mid; + if (product < x) { + ans = mid + left = mid + 1; + } else if (product > x) { + right = mid - 1; + } else if (product === x) { + return mid; + } + } + + return ans +}; + +console.log(mySqrt(200)); diff --git "a/\344\272\214\345\210\206\346\237\245\346\211\276.js" "b/\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" similarity index 100% rename from "\344\272\214\345\210\206\346\237\245\346\211\276.js" rename to "\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" From d0d7438e46f0d00472910a6374a2fb0035cfe192 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 12 May 2020 17:56:23 +0800 Subject: [PATCH 049/145] =?UTF-8?q?feat:=20=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=90=88II-113?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\276\204\346\200\273\345\220\210II-113.js" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" new file mode 100644 index 0000000..5055984 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" @@ -0,0 +1,45 @@ +let TreeNode = require('../工具/二叉树.js') + +var pathSum = function (root, sum) { + let res = []; + let search = function (node, paths) { + if (!node.val) return + paths.push(node.val); + if (node.left) { + search(node.left, paths); + } + if (node.right) { + search(node.right, paths); + } + if (!node.left && !node.right) { + if (sumVals(paths) === sum) { + res.push(paths.slice()); + } + } + paths.pop(); + }; + search(root, []); + return res; +}; + +function sumVals(nodes) { + return nodes.reduce((prev, val) => { + prev += val; + return prev; + }, 0); +} + + +var t = new TreeNode(5) +t.left = new TreeNode(4) +t.left.left = new TreeNode(11) +t.left.left.left = new TreeNode(7) +t.left.left.right = new TreeNode(2) + +t.right = new TreeNode(8) +t.right.left = new TreeNode(13) +t.right.right = new TreeNode(4) +t.right.right.left = new TreeNode(5) +t.right.right.right = new TreeNode(1) + +console.log(pathSum(new TreeNode(), 22)) From a878a997e988617e9ef1d7a37df664d4896d91a8 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 14 May 2020 20:23:55 +0800 Subject: [PATCH 050/145] feat: merge sorted array --- longest-prefix.js | 43 ------------------- ...11\345\272\217\346\225\260\347\273\204.js" | 42 ++++++++++++++++++ 2 files changed, 42 insertions(+), 43 deletions(-) delete mode 100644 longest-prefix.js create mode 100644 "\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" diff --git a/longest-prefix.js b/longest-prefix.js deleted file mode 100644 index 389f83e..0000000 --- a/longest-prefix.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * 「快乐前缀」是在原字符串中既是 非空 前缀也是后缀(不包括原字符串自身)的字符串。 - - 给你一个字符串 s,请你返回它的 最长快乐前缀。 - - 如果不存在满足题意的前缀,则返回一个空字符串。 - - 示例: - - 输入:s = 'level' - - 输出:'l' - - 解释:不包括 s 自己,一共有 4 个前缀('l', 'le', 'lev', 'leve')和 4 个后缀('l', 'el', 'vel', 'evel')。最长的既是前缀也是后缀的字符串是 'l' 。 - */ - -function makeNext(str) { - let index = 1 - let k = 0 - const len = str.length - const next = Array(len).fill(0) - for (; index < len; index += 1) { - while(k > 0 && str[index] !== str[k]) { - k = next[k - 1] - } - if (str[index] === str[k]) { - k++ - } - next[index] = k - } - return next -} - -// function longestPrefix(s) { -// const max = s.length -// const next = makeNext(s) -// if (max > 0) { -// return s.slice(0, next[max - 1]) -// } -// return '' -// } - -console.log(makeNext('leabcdleabcdlv')) \ No newline at end of file diff --git "a/\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" "b/\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" new file mode 100644 index 0000000..2117c9e --- /dev/null +++ "b/\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" @@ -0,0 +1,42 @@ +/* + * @lc app=leetcode.cn id=88 lang=javascript + * + * [88] 合并两个有序数组 + */ + +// @lc code=start +/** + * @param {number[]} nums1 + * @param {number} m + * @param {number[]} nums2 + * @param {number} n + * @return {void} Do not return anything, modify nums1 in-place instead. + */ +let merge = function (arr1, m, arr2, n) { + // 两个指针指向数组非空位置的末尾 + let i = m - 1; + let j = n - 1; + // 第三个指针指向第一个数组的末尾 填充数据 + let k = arr1.length - 1; + + while (i >= 0 && j >= 0) { + let num1 = arr1[i]; + let num2 = arr2[j]; + + if (num1 > num2) { + arr1[k] = num1; + i--; + } else { + arr1[k] = num2; + j--; + } + k--; + } + + while (j >= 0) { + arr1[k] = arr2[j]; + j--; + k--; + } +}; +// @lc code=end From 76aa837ecee0c630f60e4809a1bc739cdf1ff584 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 14 May 2020 22:09:38 +0800 Subject: [PATCH 051/145] fix: remove duplicated --- ...0\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" "b/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" index bb6ed6f..94e13aa 100644 --- "a/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" +++ "b/\345\217\214\346\214\207\351\222\210/\345\210\240\351\231\244\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\347\232\204\351\207\215\345\244\215\351\241\271-26.js" @@ -16,7 +16,7 @@ let removeDuplicates = function (nums) { // 把慢指针的位置更新,并且赋值成新的值,继续等待下一个新值。 if (fast !== slot) { j++; - nums[j] = num; + nums[j] = fast; } i++; } From 6dd69e0a11a432e9101ab4412a137e394e081650 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 15 May 2020 12:12:27 +0800 Subject: [PATCH 052/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E5=B1=82=E6=AC=A1=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\254\241\351\201\215\345\216\206 II.js" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" diff --git "a/\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" "b/\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" new file mode 100644 index 0000000..52ff27d --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" @@ -0,0 +1,40 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +let TreeNode = require('../工具/二叉树.js') + +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var levelOrderBottom = function (root) { + let res = [] + let dfs = (node, level = 0) => { + if (!node) return + + if (!res[level]) { + res[level] = [] + } + + dfs(node.left, level + 1) + dfs(node.right, level + 1) + + res[level].push(node.val) + } + + dfs(root) + return res.reverse() +}; + +var t = new TreeNode(3) +t.left = new TreeNode(9) +t.right = new TreeNode(20) +t.right.left = new TreeNode(15) +t.right.right = new TreeNode(7) + +console.log(levelOrderBottom(t)) \ No newline at end of file From 58aa4161e783a7f7b9dd98f61240420730e62309 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 16 May 2020 15:07:03 +0800 Subject: [PATCH 053/145] =?UTF-8?q?feat:=20=E6=8B=AC=E5=8F=B7=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\217\267\347\224\237\346\210\220.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" new file mode 100644 index 0000000..07735c1 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" @@ -0,0 +1,23 @@ +let generateParenthesis = function (n) { + let dp = [] + dp[0] = [''] + dp[1] = ['()'] + + for (let i = 2; i <= n; i++) { + let res = [] + for (let j = 0; j <= i - 1; j++) { + let inners = dp[j] + let outers = dp[i - 1 - j] + + for (let inner of inners) { + for (let outer of outers) { + res.push(`(${inner})${outer}`) + } + } + } + dp[i] = res + } + return dp[n] +}; + +console.log(generateParenthesis(4)) \ No newline at end of file From 4409f80f41b371be19a19fa7452a644882e092bd Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 18 May 2020 01:22:46 +0800 Subject: [PATCH 054/145] =?UTF-8?q?feat:=20=E6=9C=80=E5=B0=8F=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\267\257\345\276\204\345\222\214-64.js" | 43 ++++++++++++++++++ "\345\267\245\345\205\267/\345\240\206.js" | 44 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-64.js" create mode 100644 "\345\267\245\345\205\267/\345\240\206.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-64.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-64.js" new file mode 100644 index 0000000..8380b9f --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-64.js" @@ -0,0 +1,43 @@ +/** + * @param {number[][]} grid + * @return {number} + */ +let minPathSum = function (grid) { + let y = grid.length + if (!y) { + return 0 + } + let x = grid[0].length + + let dp = [] + for (let i = 0; i < y; i++) { + dp[i] = [] + } + + dp[0][0] = grid[0][0] + + // 第一行的基础状态 记得加上左边格子的值 + for (let j = 1; j < x; j++) { + dp[0][j] = grid[0][j] + dp[0][j - 1] + } + + // 第一列的基础状态 加上上方格子的最优解即可 + for (let i = 1; i < y; i++) { + dp[i][0] = grid[i][0] + dp[i - 1][0] + } + + // 开始求左上往右下求解 + for (let i = 1; i < grid.length; i++) { + for (let j = 1; j < grid[i].length; j++) { + let cur = grid[i][j] + let fromUp = cur + (dp[i - 1][j] !== undefined ? dp[i - 1][j]: Infinity) + let fromLeft = cur + (dp[i][j - 1] !== undefined ? dp[i][j - 1]: Infinity) + + dp[i][j] = Math.min( + fromUp, + fromLeft + ) + } + } + return dp[y - 1][x - 1] +}; \ No newline at end of file diff --git "a/\345\267\245\345\205\267/\345\240\206.js" "b/\345\267\245\345\205\267/\345\240\206.js" new file mode 100644 index 0000000..c86f68b --- /dev/null +++ "b/\345\267\245\345\205\267/\345\240\206.js" @@ -0,0 +1,44 @@ +class Heap { + arr = [null]; + + constructor(arr) { + for (let num of arr) { + debugger; + this.insert(num); + } + } + + insert(data) { + let { arr } = this; + arr.push(data); + let count = this.arr.length; + let index = count - 1; + let parentIndex = Math.round(index / 2); + while (arr[index] > arr[parentIndex] && parentIndex > 0) { + swap(arr, index, Math.round(index / 2)); + index = Math.round(index / 2); + parentIndex = Math.round(index / 2); + } + } + + removeMax() { + // 把最右下角的元素和顶部元素交换 然后堆化 + swap(this.arr, 1, this.arr.length - 1); + this.arr.length -= 1; + this.heapify() + } + + // TODO + heapify() { + + } +} + +let heap = new Heap([1, 4, 2, 5, 6, 3]); +heap.removeMax(); +console.log(heap); +function swap(arr, i, j) { + let temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +} From e7b3cb76e2b2f526f4f30b368b0e6696eb300a10 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Mon, 18 May 2020 11:10:00 +0800 Subject: [PATCH 055/145] =?UTF-8?q?feat:=20=E5=86=92=E6=B3=A1=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\272\244\346\215\242.js" | 7 +++ "\345\267\245\345\205\267/\345\240\206.js" | 44 ------------------- ...22\346\263\241\346\216\222\345\272\217.js" | 23 ++++++++++ 3 files changed, 30 insertions(+), 44 deletions(-) create mode 100644 "\345\267\245\345\205\267/\344\272\244\346\215\242.js" delete mode 100644 "\345\267\245\345\205\267/\345\240\206.js" create mode 100644 "\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" diff --git "a/\345\267\245\345\205\267/\344\272\244\346\215\242.js" "b/\345\267\245\345\205\267/\344\272\244\346\215\242.js" new file mode 100644 index 0000000..62e4e9a --- /dev/null +++ "b/\345\267\245\345\205\267/\344\272\244\346\215\242.js" @@ -0,0 +1,7 @@ +function swap(arr, i, j) { + let temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; +}; + +module.exports = swap \ No newline at end of file diff --git "a/\345\267\245\345\205\267/\345\240\206.js" "b/\345\267\245\345\205\267/\345\240\206.js" deleted file mode 100644 index c86f68b..0000000 --- "a/\345\267\245\345\205\267/\345\240\206.js" +++ /dev/null @@ -1,44 +0,0 @@ -class Heap { - arr = [null]; - - constructor(arr) { - for (let num of arr) { - debugger; - this.insert(num); - } - } - - insert(data) { - let { arr } = this; - arr.push(data); - let count = this.arr.length; - let index = count - 1; - let parentIndex = Math.round(index / 2); - while (arr[index] > arr[parentIndex] && parentIndex > 0) { - swap(arr, index, Math.round(index / 2)); - index = Math.round(index / 2); - parentIndex = Math.round(index / 2); - } - } - - removeMax() { - // 把最右下角的元素和顶部元素交换 然后堆化 - swap(this.arr, 1, this.arr.length - 1); - this.arr.length -= 1; - this.heapify() - } - - // TODO - heapify() { - - } -} - -let heap = new Heap([1, 4, 2, 5, 6, 3]); -heap.removeMax(); -console.log(heap); -function swap(arr, i, j) { - let temp = arr[i]; - arr[i] = arr[j]; - arr[j] = temp; -} diff --git "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" new file mode 100644 index 0000000..958e5bc --- /dev/null +++ "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" @@ -0,0 +1,23 @@ +const swap = require("../工具/交换"); +/** + * 冒泡排序 + * @param {number[]} arr + */ +function bubbleSort(arr) { + let n = arr.length; + if (n <= 1) return arr; + + for (let i = 0; i < n; i++) { + let flag = false; + for (let j = 0; j < n - 1 - i; j++) { + if (arr[j] > arr[j + 1]) { + swap(arr, j, j + 1); + flag = true; + } + } + if (!flag) return arr; + } + return arr; +} + +console.log(bubbleSort([4, 5, 2, 3, 1, 6, 5, 15, 2])); From 1824b4ca1a5f42b12eab4f852b9e2e3dc26bfe6f Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Mon, 18 May 2020 11:11:18 +0800 Subject: [PATCH 056/145] =?UTF-8?q?feat:=20=E5=86=92=E6=B3=A1=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\206\222\346\263\241\346\216\222\345\272\217.js" | 4 ++++ 1 file changed, 4 insertions(+) diff --git "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" index 958e5bc..4fdc15e 100644 --- "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" @@ -9,12 +9,16 @@ function bubbleSort(arr) { for (let i = 0; i < n; i++) { let flag = false; + // 从前往后冒泡 所以已经处理过的就不用再访问了 + // 并且由于每次遍历会访问j+1项,等于提前遍历了后一项 + // 所以这里的终止条件可以是n - i再减去1 for (let j = 0; j < n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { swap(arr, j, j + 1); flag = true; } } + // 如果这次循环都没有数字被交换 说明已经是排序好的数组 if (!flag) return arr; } return arr; From 021b605fe5ba56396777c3fa496d40b1274ec6aa Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 22 May 2020 02:18:47 +0800 Subject: [PATCH 057/145] =?UTF-8?q?feat:=20=E5=BF=AB=E9=80=9F=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...53\351\200\237\346\216\222\345\272\217.js" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" new file mode 100644 index 0000000..5411d8b --- /dev/null +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -0,0 +1,46 @@ +const swap = require("../工具/交换"); + +/** + * + * @param {number[]} arr + */ +function quickSort(arr) { + _quickSort(arr, 0, n - 1); +} + +/** + * 对 arr[l...r] 部分进行快速排序 + * @param {number[]} arr + * @param {number} l 左边界 + * @param {number} r 右边界 + */ +function _quickSort(arr, l, r) { + if (l >= r) { + return; + } + let p = partition(arr, l, r); + __quickSort(arr, l, p - 1); + __quickSort(arr, p + 1, r); +} + +/** + * 对 arr[l...r] 部分进行快速排序 + * @param {number[]} arr + * @param {number} l 左边界 + * @param {number} r 右边界 + * @returns {number} 返回索引值p,使得arr[l...p-1] < arr[p] < arr[p+1...r] + */ +function partition(arr, l, r) { + // 取一个基准值 取第一项 + let v = arr[l]; + + // arr[l+1...j] < v; arr[j+1...i) > v + let j = l; + for (let i = l + 1; i <= r; i++) { + if (arr[i] < v) { + // 如果当前值小于基准值的话,就交换到j + 1后的位置去。 + swap(arr, j + 1, i); + j++; + } + } +} From f47aae42485144c330b44715e075852ae40f9856 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 22 May 2020 02:19:40 +0800 Subject: [PATCH 058/145] =?UTF-8?q?feat:=20=E5=BF=AB=E9=80=9F=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\277\253\351\200\237\346\216\222\345\272\217.js" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" index 5411d8b..53acfcf 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -34,11 +34,12 @@ function partition(arr, l, r) { // 取一个基准值 取第一项 let v = arr[l]; - // arr[l+1...j] < v; arr[j+1...i) > v + // arr[l+1...j] < v < arr[j+1...i) > v let j = l; for (let i = l + 1; i <= r; i++) { if (arr[i] < v) { // 如果当前值小于基准值的话,就交换到j + 1后的位置去。 + // 扩充了j的范围 [j...], v, [...r] swap(arr, j + 1, i); j++; } From f04e6d620f5f5a3ba9db222bf82dee83287ba9d1 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 22 May 2020 12:58:02 +0800 Subject: [PATCH 059/145] =?UTF-8?q?feat:=20=E5=BF=AB=E9=80=9F=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...77\253\351\200\237\346\216\222\345\272\217.js" | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" index 53acfcf..830acd2 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -5,7 +5,8 @@ const swap = require("../工具/交换"); * @param {number[]} arr */ function quickSort(arr) { - _quickSort(arr, 0, n - 1); + _quickSort(arr, 0, arr.length - 1); + return arr; } /** @@ -19,8 +20,8 @@ function _quickSort(arr, l, r) { return; } let p = partition(arr, l, r); - __quickSort(arr, l, p - 1); - __quickSort(arr, p + 1, r); + _quickSort(arr, l, p - 1); + _quickSort(arr, p + 1, r); } /** @@ -37,11 +38,17 @@ function partition(arr, l, r) { // arr[l+1...j] < v < arr[j+1...i) > v let j = l; for (let i = l + 1; i <= r; i++) { - if (arr[i] < v) { + let num = arr[i] + if (num < v) { // 如果当前值小于基准值的话,就交换到j + 1后的位置去。 // 扩充了j的范围 [j...], v, [...r] swap(arr, j + 1, i); j++; } } + + swap(arr, l, j); + return j } + +console.log(quickSort([1, 4, 2, 5, 3])); From 26e8daa6dcca68b867000786d7d7d324048455d8 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 22 May 2020 17:00:11 +0800 Subject: [PATCH 060/145] feat: LRUCache --- .prettierrc | 3 +- "LRU\347\274\223\345\255\230.js" | 94 +++++++++++++++++++ ...11\346\213\251\346\216\222\345\272\217.js" | 16 ++++ ...7\345\255\227\347\254\246\344\270\2622.js" | 34 +++---- 4 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 "LRU\347\274\223\345\255\230.js" create mode 100644 "\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" diff --git a/.prettierrc b/.prettierrc index f04c200..6d334cc 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,4 +1,5 @@ { "printWidth": 80, - "tabWidth": 2 + "tabWidth": 2, + "semi": false } \ No newline at end of file diff --git "a/LRU\347\274\223\345\255\230.js" "b/LRU\347\274\223\345\255\230.js" new file mode 100644 index 0000000..25d09e7 --- /dev/null +++ "b/LRU\347\274\223\345\255\230.js" @@ -0,0 +1,94 @@ +class DoubleNode { + constructor(key, val) { + this.key = key + this.val = val + + this.prev = null + this.next = null + } +} + +class LRUCache { + constructor(max) { + this.max = max + this.map = new Map() + + this.head = null + this.tail = null + } + + get(key) { + const node = this.map.get(key) + if (!node) { + return -1 + } else { + const res = node.val + this.remove(node) + this.appendHead(node) + return res + } + } + + put(key, value) { + let node = this.map.get(key) + // 有这个缓存 + if (node) { + node.val = value + // 新加入的 放在最前面 + this.remove(node) + this.appendHead(node) + } else { + // 没有这个缓存 + node = new DoubleNode(key, value) + // 如果超出容量了 删除最后一个 再放到头部 + if (this.map.size >= this.max) { + this.map.delete(this.tail.key) + this.remove(this.tail) + this.appendHead(node) + this.map.set(key, node) + } else { + // 未超出容量 就直接放到头部 + this.appendHead(node) + this.map.set(key, node) + } + } + } + + /** + * 把头部指针的改变成新的node + * @param {DoubleNode} node + */ + appendHead(node) { + if (this.head === null) { + this.head = this.tail = node + } else { + node.next = this.head + this.head.prev = node + this.head = node + } + } + + /** + * 删除某个节点 + * @param {DoubleNode} node + */ + remove(node) { + if (this.head === this.tail) { + this.head = this.tail = null + } else { + // 删除头部 + if (this.head === node) { + this.head = this.head.next + node.next = null + } else if (this.tail === node) { + this.tail = this.tail.prev + this.tail.next = null + node.prev = null + } else { + node.prev.next = node.next + node.next.prev = node.prev + node.prev = node.next = null + } + } + } +} diff --git "a/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" new file mode 100644 index 0000000..b46a9e3 --- /dev/null +++ "b/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" @@ -0,0 +1,16 @@ +const swap = require("../工具/交换"); + +function selectSort(arr) { + for (let i = 0; i < arr.length; i++) { + let min = i; + for (let j = i + 1; j < arr.length; j++) { + if (arr[j] < arr[min]) { + min = j; + } + } + swap(arr, i, min); + } + return arr; +} + +console.log(selectSort([1, 4, 3, 2, 5, 6])); diff --git "a/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" "b/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" index 8a4bd17..2f1b015 100644 --- "a/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" +++ "b/\351\252\214\350\257\201\345\233\236\346\226\207\345\255\227\347\254\246\344\270\2622.js" @@ -2,36 +2,36 @@ * @param {string} s * @return {boolean} */ -let validPalindrome = function(s) { +let validPalindrome = function (s) { let i = 0; - let j = s.length - 1 - + let j = s.length - 1; + // 两个指针往中间缩进 while (i < j && s[i] === s[j]) { - i++ - j-- + i++; + j--; } // 遇到相对位置不相等了 判断删除一位后的情况 if (isPalindrome(i + 1, j)) { - return true + return true; } if (isPalindrome(i, j - 1)) { - return true + return true; } // 工具方法,用于判断字符串是否回文 function isPalindrome(st, ed) { - while(st Date: Sat, 23 May 2020 14:27:34 +0800 Subject: [PATCH 061/145] =?UTF-8?q?feat:=20=E6=95=B0=E7=BB=84=E4=BA=A4?= =?UTF-8?q?=E9=9B=86II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\232\204\344\272\244\351\233\206II-350.js" | 36 +++++++++++++++++++ ...45\255\220\346\225\260\347\273\204-209.js" | 30 ++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 "\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II-350.js" create mode 100644 "\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" diff --git "a/\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II-350.js" "b/\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II-350.js" new file mode 100644 index 0000000..be6e139 --- /dev/null +++ "b/\344\270\244\344\270\252\346\225\260\347\273\204\347\232\204\344\272\244\351\233\206II-350.js" @@ -0,0 +1,36 @@ +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ +let intersect = function (nums1, nums2) { + let map1 = makeCountMap(nums1) + let map2 = makeCountMap(nums2) + let res = [] + for (let num of map1.keys()) { + const count1 = map1.get(num) + const count2 = map2.get(num) + + if (count2) { + const pushCount = Math.min(count1, count2) + for (let i = 0; i < pushCount; i++) { + res.push(num) + } + } + } + return res +} + +function makeCountMap(nums) { + let map = new Map() + for (let i = 0; i < nums.length; i++) { + let num = nums[i] + let count = map.get(num) + if (count) { + map.set(num, count + 1) + } else { + map.set(num, 1) + } + } + return map +} diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" new file mode 100644 index 0000000..bd05c78 --- /dev/null +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" @@ -0,0 +1,30 @@ +/** + * @param {number} s + * @param {number[]} nums + * @return {number} + */ +let minSubArrayLen = function (s, nums) { + let n = nums.length + // 定义[i,...j]滑动窗口 取这个窗口里的和 + let i = 0 + let j = -1 + + let sum = 0 + let res = Infinity + + while (i < n) { + if (sum < s) { + sum += nums[++j] + } else { + sum -= nums[i] + i++ + } + + if (sum >= s) { + res = Math.min(res, j - i + 1) + } + } + return res === Infinity ? 0 : res +} + +console.log(minSubArrayLen(7, [2, 3, 1, 2, 4, 3])) From 523624d12e8d44a8fa3aff3b6d6dfda963bc9e92 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 23 May 2020 17:15:16 +0800 Subject: [PATCH 062/145] =?UTF-8?q?feat:=20=E5=8F=8D=E8=BD=AC=E9=93=BE?= =?UTF-8?q?=E8=A1=A8II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\351\223\276\350\241\250.js" | 6 ++ ...0\275\254\351\223\276\350\241\250II-92.js" | 74 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 "\345\267\245\345\205\267/\351\223\276\350\241\250.js" create mode 100644 "\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" diff --git "a/\345\267\245\345\205\267/\351\223\276\350\241\250.js" "b/\345\267\245\345\205\267/\351\223\276\350\241\250.js" new file mode 100644 index 0000000..53cc2ab --- /dev/null +++ "b/\345\267\245\345\205\267/\351\223\276\350\241\250.js" @@ -0,0 +1,6 @@ +function ListNode(val) { + this.val = val + this.next = null +} + +module.exports = ListNode \ No newline at end of file diff --git "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" new file mode 100644 index 0000000..ab69981 --- /dev/null +++ "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" @@ -0,0 +1,74 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +const ListNode = require("../工具/链表") +/** + * @param {ListNode} head + * @param {number} m + * @param {number} n + * @return {ListNode} + */ + +let reverseBetween = function (head, m, n) { + let i = 1 + let sliceStartPrev = null + let sliceStart = null + let sliceEnd = null + let cur = head + + // 记录切分起点的前一个节点,和切分终点的后一个节点 + while (i <= n) { + if (i === m - 1) { + sliceStartPrev = cur + } + if (i === m) { + sliceStart = cur + } + if (i === n) { + sliceEnd = cur + } + cur = cur.next + i++ + } + + let sliceEndNext = sliceEnd.next + // 切断切分终点的next 防止反转的时候反转过头 + sliceEnd.next = null + + const { head: slicedHead, tail: slicedTail } = reverse(sliceStart) + if (sliceStartPrev) { + // 如果需要反转的部分有前一个节点 那么只需要在中间动手脚 原样返回head节点即可 + sliceStartPrev.next = slicedHead + } else { + // 这里需要注意的是 如果没有sliceStartPrev 说明是从第一个节点就开始反转的 + // 那么我们需要手动调整head为反转后的head + head = slicedHead + } + slicedTail.next = sliceEndNext + + return head +} + +function reverse(head) { + let prev = null + let cur = head + while (cur) { + let next = cur.next + cur.next = prev + + prev = cur + cur = next + } + // 返回反转后的头尾节点 + return { head: prev, tail: head } +} + +let node = new ListNode(3) +node.next = new ListNode(5) + +console.log(JSON.stringify(reverseBetween(node, 1, 1))) From 657994f19f1e254c73315a770c590ef4aee0d152 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 23 May 2020 17:15:43 +0800 Subject: [PATCH 063/145] =?UTF-8?q?feat:=20=E5=8F=8D=E8=BD=AC=E9=93=BE?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\217\215\350\275\254\351\223\276\350\241\250.js" | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 "\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" diff --git "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" new file mode 100644 index 0000000..32097f9 --- /dev/null +++ "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" @@ -0,0 +1,13 @@ +function reverse(head) { + let prev = null + let cur = head + while (cur) { + let next = cur.next + cur.next = prev + + prev = cur + cur = next + } + // 返回反转后的头节点 + return prev +} From e24553c5cdd19cad2dd2f9c99103d1f988684a70 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 23 May 2020 18:34:54 +0800 Subject: [PATCH 064/145] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...271-\351\235\242\350\257\225\351\242\23018.js" | 15 +++++++++++++++ ...15\350\275\254\351\223\276\350\241\250-206.js" | 0 2 files changed, 15 insertions(+) create mode 100644 "\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-\351\235\242\350\257\225\351\242\23018.js" rename "\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" => "\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" (100%) diff --git "a/\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-\351\235\242\350\257\225\351\242\23018.js" "b/\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-\351\235\242\350\257\225\351\242\23018.js" new file mode 100644 index 0000000..611cb98 --- /dev/null +++ "b/\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-\351\235\242\350\257\225\351\242\23018.js" @@ -0,0 +1,15 @@ +var deleteNode = function (head, val) { + let virtual = { + next: head, + } + let cur = virtual + while (cur) { + if (cur.next) { + if (cur.next.val === val) { + cur.next = cur.next.next + } + } + cur = cur.next + } + return virtual.next +} diff --git "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" similarity index 100% rename from "\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250.js" rename to "\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" From 6921d96df0b6f95b98b2a2046b96215930ea1a11 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 26 May 2020 10:43:45 +0800 Subject: [PATCH 065/145] =?UTF-8?q?feat:=20=E6=95=B4=E5=90=88=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E6=B5=8B=E9=80=9F=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 22 ++++++ package.json | 5 ++ sum-four-divisors.js | 34 --------- ...22\345\272\217\351\200\237\345\272\246.js" | 74 +++++++++++++++++++ ...22\346\263\241\346\216\222\345\272\217.js" | 20 ++--- ...53\351\200\237\346\216\222\345\272\217.js" | 42 ++++++----- ...11\346\213\251\346\216\222\345\272\217.js" | 14 ++-- 7 files changed, 142 insertions(+), 69 deletions(-) create mode 100644 .gitignore create mode 100644 package.json delete mode 100644 sum-four-divisors.js create mode 100644 "\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..931e226 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +.DS_Store +._* +.cache +.project +.settings +.tmproj +*.esproj +*.sublime-project +*.sublime-workspace +nbproject +thumbs.db +*.iml + +# Folders to ignore +.hg +.svn +.CVS +.idea +node_modules/ +jscoverage_lib/ +bower_components/ +dist/ \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3c48796 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "glob": "^7.1.6" + } +} diff --git a/sum-four-divisors.js b/sum-four-divisors.js deleted file mode 100644 index aeaa8d5..0000000 --- a/sum-four-divisors.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * @param {number[]} nums - * @return {number} - */ -let sumFourDivisors = function(nums) { - let sum = 0 - for (let i = 0; i < nums.length; i++) { - let divisorSet = findDivisor(nums[i]) - if (divisorSet.size === 4) { - divisorSet.forEach(num => { - sum += num - }) - } - } - return sum -}; - -function findDivisor(num) { - // 超过这个边界就不用继续求值了 - let max = Math.floor(Math.sqrt(num)) - let set = new Set() - for (let i = 1; i <= max; i ++) { - let result = num / i - // 除数是整数 - if (result % 1 === 0) { - set.add(result) - // 除以结果 是另一个因数 - set.add(num / result) - } - } - return set -} - -console.log(sumFourDivisors([21,4,7])) \ No newline at end of file diff --git "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" new file mode 100644 index 0000000..4e6eb31 --- /dev/null +++ "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" @@ -0,0 +1,74 @@ +const glob = require("glob") +const path = require("path") +const swap = require("./交换") + +function sortTest(sortFns, source, desc) { + console.log(desc) + const table = {} + + if (typeof source === "function") { + source = source() + } + + sortFns.forEach((fn) => { + const copy = source.slice() + const start = new Date().getTime() + + fn(copy) + + const end = new Date().getTime() + const time = end - start + const timeStr = `${time}ms` + const success = isSorted(copy, source) + + table[fn.sortName] = { + 耗时: timeStr, + 数据长度: source.length, + 结果: success ? "成功" : "失败", + } + }) + + console.table(table) +} + +function getRandomArray(count) { + const arr = [] + for (let i = 0; i < count; i++) { + arr.push(Math.floor(i * Math.random() * 10)) + } + + return arr +} + +function getNearlyArray(count, swapTime) { + const arr = [] + for (let i = 0; i < count; i++) { + arr.push(i) + } + + for (let i = 0; i < swapTime; i++) { + const x = Math.floor(Math.random() * count) + const y = Math.floor(Math.random() * count) + swap(arr, x, y) + } + + return arr +} + +function isSorted(target, source) { + return ( + target.toString() === + source + .slice() + .sort((a, b) => a - b) + .toString() + ) +} + +glob("排序/*.js", (err, result) => { + if (err) throw err + const sortFunctions = result + .map((p) => require(path.resolve(p))) + .filter(Boolean) + sortTest(sortFunctions, () => getRandomArray(10000), "普通数组排序") +}) diff --git "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" index 4fdc15e..6293e20 100644 --- "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" @@ -1,27 +1,29 @@ -const swap = require("../工具/交换"); +const swap = require("../工具/交换") /** * 冒泡排序 * @param {number[]} arr */ function bubbleSort(arr) { - let n = arr.length; - if (n <= 1) return arr; + let n = arr.length + if (n <= 1) return arr for (let i = 0; i < n; i++) { - let flag = false; + let flag = false // 从前往后冒泡 所以已经处理过的就不用再访问了 // 并且由于每次遍历会访问j+1项,等于提前遍历了后一项 // 所以这里的终止条件可以是n - i再减去1 for (let j = 0; j < n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { - swap(arr, j, j + 1); - flag = true; + swap(arr, j, j + 1) + flag = true } } // 如果这次循环都没有数字被交换 说明已经是排序好的数组 - if (!flag) return arr; + if (!flag) return arr } - return arr; + return arr } -console.log(bubbleSort([4, 5, 2, 3, 1, 6, 5, 15, 2])); +bubbleSort.sortName = "冒泡排序" + +module.exports = bubbleSort diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" index 830acd2..3fb0f57 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -1,12 +1,12 @@ -const swap = require("../工具/交换"); +const swap = require("../工具/交换") /** * * @param {number[]} arr */ function quickSort(arr) { - _quickSort(arr, 0, arr.length - 1); - return arr; + _quickSort(arr, 0, arr.length - 1) + return arr } /** @@ -17,11 +17,11 @@ function quickSort(arr) { */ function _quickSort(arr, l, r) { if (l >= r) { - return; + return } - let p = partition(arr, l, r); - _quickSort(arr, l, p - 1); - _quickSort(arr, p + 1, r); + let p = partition(arr, l, r) + _quickSort(arr, l, p - 1) + _quickSort(arr, p + 1, r) } /** @@ -31,24 +31,26 @@ function _quickSort(arr, l, r) { * @param {number} r 右边界 * @returns {number} 返回索引值p,使得arr[l...p-1] < arr[p] < arr[p+1...r] */ -function partition(arr, l, r) { +function partition(arr, left, right) { // 取一个基准值 取第一项 - let v = arr[l]; + let pivot = arr[left] - // arr[l+1...j] < v < arr[j+1...i) > v - let j = l; - for (let i = l + 1; i <= r; i++) { + // arr[left+1...index] < pivot, arr[index+1...i) > pivot + let index = left + for (let i = left + 1; i <= right; i++) { let num = arr[i] - if (num < v) { - // 如果当前值小于基准值的话,就交换到j + 1后的位置去。 - // 扩充了j的范围 [j...], v, [...r] - swap(arr, j + 1, i); - j++; + if (num < pivot) { + // 如果当前值小于基准值的话,就交换到index + 1的位置去。 + // 扩充了index的范围 [index...], pivot, [...right] + swap(arr, index + 1, i) + index++ } } - swap(arr, l, j); - return j + swap(arr, left, index) + return index } -console.log(quickSort([1, 4, 2, 5, 3])); +quickSort.sortName = "快速排序" + +module.exports = quickSort diff --git "a/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" index b46a9e3..c51d86b 100644 --- "a/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" @@ -1,16 +1,18 @@ -const swap = require("../工具/交换"); +const swap = require("../工具/交换") function selectSort(arr) { for (let i = 0; i < arr.length; i++) { - let min = i; + let min = i for (let j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) { - min = j; + min = j } } - swap(arr, i, min); + swap(arr, i, min) } - return arr; + return arr } -console.log(selectSort([1, 4, 3, 2, 5, 6])); +selectSort.sortName = "选择排序" + +module.exports = selectSort From 317df3d2ad1d9152f414b7aa62bb52226ea2792e Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 26 May 2020 15:59:28 +0800 Subject: [PATCH 066/145] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=A0=87?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\213\254\345\217\267\347\224\237\346\210\220-22.js" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" => "\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" (100%) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" similarity index 100% rename from "\345\212\250\346\200\201\350\247\204\345\210\222/22.\346\213\254\345\217\267\347\224\237\346\210\220.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" From 990927121b0b474348510ba0176061a687bc67e8 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Wed, 27 May 2020 13:02:35 +0800 Subject: [PATCH 067/145] =?UTF-8?q?feat:=20=E9=9A=8F=E6=9C=BA=E5=80=BC?= =?UTF-8?q?=E5=BF=AB=E9=80=9F=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- report.20200527.011151.36368.0.001.json | 443 ++++++++++++++++++ ...04\345\255\220\346\225\260\347\273\204.js" | 33 ++ ...22\345\272\217\351\200\237\345\272\246.js" | 1 + .../\351\232\217\346\234\272\345\200\274.js" | 5 + ...53\351\200\237\346\216\222\345\272\217.js" | 3 + 5 files changed, 485 insertions(+) create mode 100644 report.20200527.011151.36368.0.001.json create mode 100644 "\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" create mode 100644 "\345\267\245\345\205\267/\351\232\217\346\234\272\345\200\274.js" diff --git a/report.20200527.011151.36368.0.001.json b/report.20200527.011151.36368.0.001.json new file mode 100644 index 0000000..1257f6a --- /dev/null +++ b/report.20200527.011151.36368.0.001.json @@ -0,0 +1,443 @@ + +{ + "header": { + "reportVersion": 1, + "event": "Allocation failed - JavaScript heap out of memory", + "trigger": "FatalError", + "filename": "report.20200527.011151.36368.0.001.json", + "dumpEventTime": "2020-05-27T01:11:51Z", + "dumpEventTimeStamp": "1590513111450", + "processId": 36368, + "cwd": "c:\\codes\\leetcode-javascript", + "commandLine": [ + "node", + "c:\\codes\\leetcode-javascript\\可被K整除的子数组.js" + ], + "nodejsVersion": "v12.13.1", + "wordSize": 64, + "arch": "x64", + "platform": "win32", + "componentVersions": { + "node": "12.13.1", + "v8": "7.7.299.13-node.16", + "uv": "1.33.1", + "zlib": "1.2.11", + "brotli": "1.0.7", + "ares": "1.15.0", + "modules": "72", + "nghttp2": "1.39.2", + "napi": "5", + "llhttp": "1.1.4", + "http_parser": "2.8.0", + "openssl": "1.1.1d", + "cldr": "35.1", + "icu": "64.2", + "tz": "2019c", + "unicode": "12.1" + }, + "release": { + "name": "node", + "lts": "Erbium", + "headersUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1-headers.tar.gz", + "sourceUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1.tar.gz", + "libUrl": "https://nodejs.org/download/release/v12.13.1/win-x64/node.lib" + }, + "osName": "Windows_NT", + "osRelease": "10.0.18362", + "osVersion": "Windows 10 Home China", + "osMachine": "x86_64", + "cpus": [ + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1276796, + "nice": 0, + "sys": 1216546, + "idle": 21543937, + "irq": 492109 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 584421, + "nice": 0, + "sys": 287875, + "idle": 23164468, + "irq": 26578 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 5303734, + "nice": 0, + "sys": 1111203, + "idle": 17621828, + "irq": 19171 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 959921, + "nice": 0, + "sys": 272703, + "idle": 22804140, + "irq": 4656 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 2716625, + "nice": 0, + "sys": 695484, + "idle": 20624656, + "irq": 16171 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 817859, + "nice": 0, + "sys": 212250, + "idle": 23006656, + "irq": 5984 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 2039062, + "nice": 0, + "sys": 662062, + "idle": 21335625, + "irq": 18093 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 681687, + "nice": 0, + "sys": 249515, + "idle": 23105546, + "irq": 5906 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1234406, + "nice": 0, + "sys": 382343, + "idle": 22420000, + "irq": 12562 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 833421, + "nice": 0, + "sys": 332765, + "idle": 22870546, + "irq": 10593 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 1193562, + "nice": 0, + "sys": 432562, + "idle": 22410625, + "irq": 15937 + }, + { + "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", + "speed": 2592, + "user": 702109, + "nice": 0, + "sys": 308437, + "idle": 23026203, + "irq": 8187 + } + ], + "networkInterfaces": [ + { + "name": "Loopback Pseudo-Interface 1", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "::1", + "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", + "family": "IPv6", + "scopeid": 0 + }, + { + "name": "Loopback Pseudo-Interface 1", + "internal": true, + "mac": "00:00:00:00:00:00", + "address": "127.0.0.1", + "netmask": "255.0.0.0", + "family": "IPv4" + }, + { + "name": "以太网", + "internal": false, + "mac": "04:d4:c4:e1:d1:af", + "address": "192.168.125.124", + "netmask": "255.255.255.0", + "family": "IPv4" + } + ], + "host": "LAPTOP-FMHNK1K6" + }, + "javascriptStack": { + "message": "No stack.", + "stack": [ + "Unavailable." + ] + }, + "nativeStack": [ + { + "pc": "0x00007ff78cae1729", + "symbol": "std::basic_ostream >::operator<<+10873" + }, + { + "pc": "0x00007ff78cae5b4c", + "symbol": "std::basic_ostream >::operator<<+28316" + }, + { + "pc": "0x00007ff78cae4b08", + "symbol": "std::basic_ostream >::operator<<+24152" + }, + { + "pc": "0x00007ff78cbd369b", + "symbol": "v8::base::CPU::has_sse+37723" + }, + { + "pc": "0x00007ff78d3d82de", + "symbol": "v8::Isolate::ReportExternalAllocationLimitReached+94" + }, + { + "pc": "0x00007ff78d3c0321", + "symbol": "v8::SharedArrayBuffer::Externalize+833" + }, + { + "pc": "0x00007ff78d28dbec", + "symbol": "v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436" + }, + { + "pc": "0x00007ff78d2a3c0d", + "symbol": "v8::internal::Factory::AllocateRawFixedArray+61" + }, + { + "pc": "0x00007ff78d2aafc4", + "symbol": "v8::internal::Factory::NewFixedArrayWithFiller+52" + }, + { + "pc": "0x00007ff78d2aaf81", + "symbol": "v8::internal::Factory::NewUninitializedFixedArray+65" + }, + { + "pc": "0x00007ff78d18c12f", + "symbol": "v8::Object::GetIsolate+7663" + }, + { + "pc": "0x00007ff78d03f13a", + "symbol": "v8::internal::OrderedHashMap::ValueAt+61274" + }, + { + "pc": "0x00007ff78d80404d", + "symbol": "v8::internal::SetupIsolateDelegate::SetupHeap+567949" + }, + { + "pc": "0x000001792d48493d", + "symbol": "" + } + ], + "javascriptHeap": { + "totalMemory": 939016192, + "totalCommittedMemory": 939016192, + "usedMemory": 907197656, + "availableMemory": 1289381792, + "memoryLimit": 2197815296, + "heapSpaces": { + "read_only_space": { + "memorySize": 262144, + "committedMemory": 262144, + "capacity": 261872, + "used": 32296, + "available": 229576 + }, + "new_space": { + "memorySize": 33554432, + "committedMemory": 33554432, + "capacity": 16759808, + "used": 3166504, + "available": 13593304 + }, + "old_space": { + "memorySize": 1945600, + "committedMemory": 1945600, + "capacity": 1151592, + "used": 1151592, + "available": 0 + }, + "code_space": { + "memorySize": 425984, + "committedMemory": 425984, + "capacity": 155232, + "used": 155232, + "available": 0 + }, + "map_space": { + "memorySize": 266240, + "committedMemory": 266240, + "capacity": 177600, + "used": 177600, + "available": 0 + }, + "large_object_space": { + "memorySize": 902512640, + "committedMemory": 902512640, + "capacity": 902510880, + "used": 902510880, + "available": 0 + }, + "code_large_object_space": { + "memorySize": 49152, + "committedMemory": 49152, + "capacity": 3552, + "used": 3552, + "available": 0 + }, + "new_large_object_space": { + "memorySize": 0, + "committedMemory": 0, + "capacity": 16759808, + "used": 0, + "available": 16759808 + } + } + }, + "resourceUsage": { + "userCpuSeconds": 3.875, + "kernelCpuSeconds": 1.187, + "cpuConsumptionPercent": 126.55, + "maxRss": 1558552576, + "pageFaults": { + "IORequired": 683613, + "IONotRequired": 0 + }, + "fsActivity": { + "reads": 2, + "writes": 88 + } + }, + "libuv": [ + ], + "environmentVariables": { + "=C:": "c:\\codes\\leetcode-javascript", + "ALLUSERSPROFILE": "C:\\ProgramData", + "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", + "APPDATA": "C:\\Users\\94958\\AppData\\Roaming", + "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", + "CommonProgramFiles": "C:\\Program Files\\Common Files", + "CommonProgramFiles(x86)": "C:\\Program Files (x86)\\Common Files", + "CommonProgramW6432": "C:\\Program Files\\Common Files", + "COMPUTERNAME": "LAPTOP-FMHNK1K6", + "ComSpec": "C:\\Windows\\system32\\cmd.exe", + "DriverData": "C:\\Windows\\System32\\Drivers\\DriverData", + "ELECTRON_RUN_AS_NODE": "1", + "GameFirst": "C:\\Program Files (x86)\\ASUS\\GameFirst\\", + "GameFirstUserPath": "C:\\Users\\94958\\AppData\\Local\\ASUS\\GameFirst", + "HOMEDRIVE": "C:", + "HOMEPATH": "\\Users\\94958", + "LOCALAPPDATA": "C:\\Users\\94958\\AppData\\Local", + "LOGONSERVER": "\\\\LAPTOP-FMHNK1K6", + "NUMBER_OF_PROCESSORS": "12", + "OneDrive": "C:\\Users\\94958\\OneDrive", + "OneDriveConsumer": "C:\\Users\\94958\\OneDrive", + "OS": "Windows_NT", + "Path": "C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\nodejs\\;C:\\Users\\94958\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\94958\\AppData\\Roaming\\npm;C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", + "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", + "PIPE_LOGGING": "true", + "PROCESSOR_ARCHITECTURE": "AMD64", + "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 10, GenuineIntel", + "PROCESSOR_LEVEL": "6", + "PROCESSOR_REVISION": "9e0a", + "ProgramData": "C:\\ProgramData", + "ProgramFiles": "C:\\Program Files", + "ProgramFiles(x86)": "C:\\Program Files (x86)", + "ProgramW6432": "C:\\Program Files", + "PROMPT": "$P$G", + "PSModulePath": "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules", + "PUBLIC": "C:\\Users\\Public", + "SESSIONNAME": "Console", + "SystemDrive": "C:", + "SystemRoot": "C:\\Windows", + "TEMP": "C:\\Users\\94958\\AppData\\Local\\Temp", + "TMP": "C:\\Users\\94958\\AppData\\Local\\Temp", + "USERDOMAIN": "LAPTOP-FMHNK1K6", + "USERDOMAIN_ROAMINGPROFILE": "LAPTOP-FMHNK1K6", + "USERNAME": "94958", + "USERPROFILE": "C:\\Users\\94958", + "VERBOSE_LOGGING": "true", + "VSCODE_CWD": "C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code", + "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", + "VSCODE_IPC_HOOK": "\\\\.\\pipe\\b8cbe1eb44c59330f23b7126ffc1d83e-1.45.1-main-sock", + "VSCODE_IPC_HOOK_EXTHOST": "\\\\.\\pipe\\vscode-ipc-31fc3b14-6b96-4af5-aa86-38e0a0760941-sock", + "VSCODE_LOGS": "C:\\Users\\94958\\AppData\\Roaming\\Code\\logs\\20200527T010954", + "VSCODE_LOG_STACK": "false", + "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"2feefd28b9e8042903942873399b35a3.zh-cn\",\"_translationsConfigFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\\\\tcf.json\",\"_cacheRoot\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\\\\5763d909d5f12fe19f215cbfdd29a91c0fa9208a\",\"_corruptedFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\\\\corrupted.info\",\"_languagePackSupport\":true}", + "VSCODE_NODE_CACHED_DATA_DIR": "C:\\Users\\94958\\AppData\\Roaming\\Code\\CachedData\\5763d909d5f12fe19f215cbfdd29a91c0fa9208a", + "VSCODE_PID": "33348", + "windir": "C:\\Windows" + }, + "sharedObjects": [ + "C:\\Program Files\\nodejs\\node.exe", + "C:\\Windows\\SYSTEM32\\ntdll.dll", + "C:\\Windows\\System32\\KERNEL32.DLL", + "C:\\Windows\\System32\\KERNELBASE.dll", + "C:\\Windows\\System32\\WS2_32.dll", + "C:\\Windows\\System32\\RPCRT4.dll", + "C:\\Windows\\SYSTEM32\\dbghelp.dll", + "C:\\Windows\\System32\\ADVAPI32.dll", + "C:\\Windows\\System32\\ucrtbase.dll", + "C:\\Windows\\System32\\msvcrt.dll", + "C:\\Windows\\System32\\sechost.dll", + "C:\\Windows\\System32\\USER32.dll", + "C:\\Windows\\System32\\win32u.dll", + "C:\\Windows\\System32\\GDI32.dll", + "C:\\Windows\\System32\\gdi32full.dll", + "C:\\Windows\\System32\\msvcp_win.dll", + "C:\\Windows\\System32\\PSAPI.DLL", + "C:\\Windows\\System32\\CRYPT32.dll", + "C:\\Windows\\System32\\MSASN1.dll", + "C:\\Windows\\System32\\bcrypt.dll", + "C:\\Windows\\SYSTEM32\\USERENV.dll", + "C:\\Windows\\SYSTEM32\\IPHLPAPI.DLL", + "C:\\Windows\\System32\\profapi.dll", + "C:\\Windows\\SYSTEM32\\WINMM.dll", + "C:\\Windows\\SYSTEM32\\winmmbase.dll", + "C:\\Windows\\System32\\cfgmgr32.dll", + "C:\\Windows\\System32\\bcryptPrimitives.dll", + "C:\\Windows\\System32\\IMM32.DLL", + "C:\\Windows\\System32\\powrprof.dll", + "C:\\Windows\\System32\\UMPDC.dll", + "C:\\Windows\\SYSTEM32\\CRYPTBASE.DLL", + "C:\\Windows\\system32\\uxtheme.dll", + "C:\\Windows\\System32\\combase.dll", + "C:\\Windows\\SysWOW64\\ierd_tgp_lsp64.dll", + "C:\\Windows\\System32\\SHLWAPI.dll", + "C:\\Windows\\System32\\ole32.dll", + "C:\\Windows\\system32\\mswsock.dll", + "C:\\Windows\\system32\\wshqos.dll", + "C:\\Windows\\SYSTEM32\\wshtcpip.DLL", + "C:\\Windows\\SYSTEM32\\wship6.dll", + "C:\\Windows\\System32\\kernel.appcore.dll", + "C:\\Windows\\System32\\NSI.dll", + "C:\\Windows\\SYSTEM32\\dhcpcsvc6.DLL", + "C:\\Windows\\SYSTEM32\\dhcpcsvc.DLL", + "C:\\Windows\\SYSTEM32\\DNSAPI.dll", + "C:\\Windows\\system32\\napinsp.dll", + "C:\\Windows\\system32\\pnrpnsp.dll", + "C:\\Windows\\System32\\winrnr.dll", + "C:\\Windows\\system32\\NLAapi.dll", + "C:\\Windows\\system32\\wshbth.dll" + ] +} \ No newline at end of file diff --git "a/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" "b/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" new file mode 100644 index 0000000..33a3c7c --- /dev/null +++ "b/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" @@ -0,0 +1,33 @@ +/** + * 暴力解 超时了 + * @param {number[]} A + * @param {number} K + * @return {number} + */ +var subarraysDivByK = function (A, K) { + let prevPrefix = [] + let currentPrefix = [] + let count = 0 + for (let i = A.length - 1; i >= 0; i--) { + let num = A[i] + judge(num) + currentPrefix.push(num) + for (let prev of prevPrefix) { + let sum = prev + num + judge(sum) + currentPrefix.push(sum) + } + prevPrefix = currentPrefix + currentPrefix = [] + } + + return count + + function judge(num) { + if (num % K === 0 || num === 0) { + count++ + } + } +} + +console.log(subarraysDivByK([4,5,0,-2,-3,1], 5)) diff --git "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" index 4e6eb31..ae44344 100644 --- "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" +++ "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" @@ -71,4 +71,5 @@ glob("排序/*.js", (err, result) => { .map((p) => require(path.resolve(p))) .filter(Boolean) sortTest(sortFunctions, () => getRandomArray(10000), "普通数组排序") + sortTest(sortFunctions, () => getNearlyArray(50000), "近似数组排序") }) diff --git "a/\345\267\245\345\205\267/\351\232\217\346\234\272\345\200\274.js" "b/\345\267\245\345\205\267/\351\232\217\346\234\272\345\200\274.js" new file mode 100644 index 0000000..afd8643 --- /dev/null +++ "b/\345\267\245\345\205\267/\351\232\217\346\234\272\345\200\274.js" @@ -0,0 +1,5 @@ +function random(low, high) { + return Math.round(Math.random() * (high - low)) + low +} + +module.exports = random diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" index 3fb0f57..2cc1dae 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -1,4 +1,5 @@ const swap = require("../工具/交换") +const random = require("../工具/随机值") /** * @@ -33,6 +34,8 @@ function _quickSort(arr, l, r) { */ function partition(arr, left, right) { // 取一个基准值 取第一项 + let rand = random(left, right) + swap(arr, left, rand) let pivot = arr[left] // arr[left+1...index] < pivot, arr[index+1...i) > pivot From f89ebdb3b20cf586b6d746b5cba71eb9e09af1af Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 28 May 2020 04:01:33 +0800 Subject: [PATCH 068/145] =?UTF-8?q?feat:=20=E5=BF=AB=E6=8E=92=E7=A9=BA?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...04\345\255\220\346\225\260\347\273\204.js" | 9 ++++--- ...22\345\272\217\351\200\237\345\272\246.js" | 13 ++++++++-- ...2\345\272\217-\347\251\272\351\227\264.js" | 24 +++++++++++++++++++ ...53\351\200\237\346\216\222\345\272\217.js" | 2 +- 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 "\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" diff --git "a/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" "b/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" index 33a3c7c..8349671 100644 --- "a/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" +++ "b/\345\217\257\350\242\253K\346\225\264\351\231\244\347\232\204\345\255\220\346\225\260\347\273\204.js" @@ -11,23 +11,22 @@ var subarraysDivByK = function (A, K) { for (let i = A.length - 1; i >= 0; i--) { let num = A[i] judge(num) - currentPrefix.push(num) for (let prev of prevPrefix) { let sum = prev + num judge(sum) - currentPrefix.push(sum) } prevPrefix = currentPrefix currentPrefix = [] } - return count - function judge(num) { if (num % K === 0 || num === 0) { count++ } + currentPrefix.push(num) } + + return count } -console.log(subarraysDivByK([4,5,0,-2,-3,1], 5)) +console.log(subarraysDivByK([4, 5, 0, -2, -3, 1], 5)) diff --git "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" index ae44344..0156435 100644 --- "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" +++ "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" @@ -1,6 +1,7 @@ const glob = require("glob") const path = require("path") const swap = require("./交换") +const random = require("./随机值") function sortTest(sortFns, source, desc) { console.log(desc) @@ -36,7 +37,6 @@ function getRandomArray(count) { for (let i = 0; i < count; i++) { arr.push(Math.floor(i * Math.random() * 10)) } - return arr } @@ -55,6 +55,14 @@ function getNearlyArray(count, swapTime) { return arr } +function getRangedArray(count, min, max) { + const arr = [] + for (let i = 0; i < count; i++) { + arr.push(random(min, max)) + } + return arr +} + function isSorted(target, source) { return ( target.toString() === @@ -71,5 +79,6 @@ glob("排序/*.js", (err, result) => { .map((p) => require(path.resolve(p))) .filter(Boolean) sortTest(sortFunctions, () => getRandomArray(10000), "普通数组排序") - sortTest(sortFunctions, () => getNearlyArray(50000), "近似数组排序") + sortTest(sortFunctions, () => getNearlyArray(10000), "近似数组排序") + sortTest(sortFunctions, () => getRangedArray(10000), "大量重复值元素排序") }) diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" new file mode 100644 index 0000000..023fcad --- /dev/null +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" @@ -0,0 +1,24 @@ +function quickSort(arr) { + if (arr.length === 1 || arr.length === 0) { + return arr + } + const left = [] + + const right = [] + const ref = arr[0] + + for (let i = 1; i < arr.length; i++) { + let num = arr[i] + if (num < ref) { + left.push(num) + } else { + right.push(num) + } + } + + return [...quickSort(left), ref, ...quickSort(right)] +} + +quickSort.sortName = "快速排序(空间版)" + +module.exports = quickSort diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" index 2cc1dae..b41a667 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -33,7 +33,7 @@ function _quickSort(arr, l, r) { * @returns {number} 返回索引值p,使得arr[l...p-1] < arr[p] < arr[p+1...r] */ function partition(arr, left, right) { - // 取一个基准值 取第一项 + // 取一个基准值 取随机值 let rand = random(left, right) swap(arr, left, rand) let pivot = arr[left] From 6e8944cacede626d369a43b001692f261edb360a Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 28 May 2020 17:37:07 +0800 Subject: [PATCH 069/145] =?UTF-8?q?feat:=20=E5=8F=AF=E8=8E=B7=E5=BE=97?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E7=82=B9=E6=95=B0dp=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\244\247\347\202\271\346\225\260-1423.js" | 45 ++++++++++++++++ ...22\345\272\217\351\200\237\345\272\246.js" | 9 +++- ...2\345\272\217-\344\270\211\350\267\257.js" | 51 +++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" create mode 100644 "\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" new file mode 100644 index 0000000..12a63d3 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" @@ -0,0 +1,45 @@ +// 力扣超时 卡在第26个用例 +let maxScore = function (cardPoints, k) { + let n = cardPoints.length + + let prevTimeChunk = [] + for (let i = 0; i < n; i++) { + for (let j = i; j < n; j++) { + if (!prevTimeChunk[i]) { + prevTimeChunk[i] = [] + } + prevTimeChunk[i][j] = 0 + } + } + + let currentTimeChunk = [] + + for (let time = 1; time <= k; time++) { + for (let i = n - 1; i >= 0; i--) { + for (let j = i; j < n; j++) { + if (!currentTimeChunk[i]) { + currentTimeChunk[i] = [] + } + + // 只剩一个可选 有次数的情况下就选这一项 否则为0 + if (i === j) { + currentTimeChunk[i][j] = time > 0 ? cardPoints[i] : 0 + } + + let pickHead = cardPoints[i] + let pickTail = cardPoints[j] + + currentTimeChunk[i][j] = Math.max( + pickHead + (prevTimeChunk[i + 1] ? prevTimeChunk[i + 1][j] || 0 : 0), + pickTail + (prevTimeChunk[i][j - 1] || 0) + ) + } + } + prevTimeChunk = currentTimeChunk + currentTimeChunk = [] + } + + return prevTimeChunk[0][n - 1] +} + +console.log(maxScore([1,79,80,1,1,1,200,1], 3)) diff --git "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" index 0156435..0b4e20a 100644 --- "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" +++ "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" @@ -15,7 +15,14 @@ function sortTest(sortFns, source, desc) { const copy = source.slice() const start = new Date().getTime() - fn(copy) + try { + fn(copy) + } catch (e) { + return (table[fn.sortName] = { + 结果: "程序异常", + 原因: e.message, + }) + } const end = new Date().getTime() const time = end - start diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" new file mode 100644 index 0000000..c77baf8 --- /dev/null +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" @@ -0,0 +1,51 @@ +const swap = require("../工具/交换") +const random = require("../工具/随机值") + +/** + * 三路快速排序 + * 将 arr[l...r] 分为 < v, === v, > v三部分 + * 之后递归的对 < v, > v 两部分三路快排 + * @param {number[]} arr + */ +function quickSort(arr) { + _quickSort(arr, 0, arr.length - 1) + return arr +} + +/** + * 对 arr[l...r] 部分进行快速排序 + * @param {number[]} arr + * @param {number} l 左边界 + * @param {number} r 右边界 + */ +function _quickSort(arr, l, r) { + if (l >= r) { + return + } + let [p, q] = partition(arr, l, r) + _quickSort(arr, l, p) + _quickSort(arr, q, r) +} + +/** + * 对 arr[l...r] 部分进行快速排序 + * @param {number[]} arr + * @param {number} l 左边界 + * @param {number} r 右边界 + * @returns {number} 返回索引值p,使得arr[l...p-1] < arr[p] < arr[p+1...r] + */ +function partition(arr, left, right) { + // 取一个基准值 取随机值 + let rand = random(left, right) + swap(arr, left, rand) + let pivot = arr[left] + + // 三路 注意看注释里的区间 + let lt = left // arr[left + 1...lt] < v + let gt = right + 1 // arr[gt...r] > v + let index = left + 1 // arr[lt + 1...index] === v +} + +quickSort.sortName = "快速排序(三路)" + +module.exports = quickSort From 7f309d4a0786222e42a1abd98811e638692fd220 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 30 May 2020 21:12:40 +0800 Subject: [PATCH 070/145] =?UTF-8?q?feat:=20=E6=9C=80=E5=B0=8F=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\347\252\227\345\217\243\357\274\211.js" | 72 ------------------- ...45\274\202\344\275\215\350\257\215-438.js" | 49 +++++++++++++ ...\351\225\277\345\255\220\344\270\262-3.js" | 28 ++++++++ ...06\347\233\226\345\255\220\344\270\262.js" | 60 ++++++++++++++++ 4 files changed, 137 insertions(+), 72 deletions(-) delete mode 100644 "\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" create mode 100644 "\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" create mode 100644 "\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" create mode 100644 "\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" diff --git "a/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" "b/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" deleted file mode 100644 index eeb11e8..0000000 --- "a/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262\357\274\210\346\273\221\345\212\250\347\252\227\345\217\243\357\274\211.js" +++ /dev/null @@ -1,72 +0,0 @@ -/** - * 3. 无重复字符的最长子串 - * 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 - - 示例 1: - - 输入: "abcabcbb" - 输出: 3 - 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 - 示例 2: - - 输入: "bbbbb" - 输出: 1 - 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 - 示例 3: - - 输入: "pwwkew" - 输出: 3 - 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 -   请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。 - - 来源:力扣(LeetCode) - 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters - 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 -*/ -let lengthOfLongestSubstring = function(str) { - if (str.length === 1) { - return 1 - } - // 滑动窗口 - let windows = [0, 0] - let max = 0 - - for (let i = 0; i < str.length; i++) { - let char = str[i] - - // 当前窗口中的文字 - let windowsStr = str.substring(windows[0], windows[1]) - - // 窗口中如果已经出现了这个文字 就需要把窗口左侧移动到「上一次出现这个文字」的右边位置 - let windowsCharIdx = windowsStr.indexOf(char) - if (windowsCharIdx !== -1) { - // 注意要加上窗口左侧已经移动的距离 - windows[0] += windowsCharIdx + 1 - } - - // 右边界始终后移 - windows[1]++ - - let windowsLength = windows[1] - windows[0] - if (windowsLength > max) { - max = windowsLength - } - } - - return max -} -console.log(lengthOfLongestSubstring("abcabcbb")) - -/** - * 参考 - * 什么是「滑动窗口算法」(sliding window algorithm),有哪些应用场景? - 程序员吴师兄的回答 - 知乎 - https://www.zhihu.com/question/314669016/answer/663930108 - - 注意边界情况很坑,比如'pwwkew'这种,在i = 2遇到第二个w的时候,窗口中的文字是pw, - - 要把左边界移动到第一个w的右边一位,也就是[2, 2]再继续开始匹配,此时会从第二个w继续向右滑动。 - - 再比如'abcdbacd'这种,在i = 4遇到第二个b的时候,窗口中是[a, b, c, d],此时也不能只单纯向后移动一位,而是需要后移到第一个b的右边 - - 也就是[2, 4]再开始匹配, 也就是从[c, d, b] 再继续往后查找最大的子串。 - */ \ No newline at end of file diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" new file mode 100644 index 0000000..7394377 --- /dev/null +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" @@ -0,0 +1,49 @@ +/** + * @param {string} s + * @param {string} p + * @return {number[]} + */ +let findAnagrams = function (s, p) { + let sl = s.length + let pl = p.length + let last = sl - pl + 1 + + let res = [] + for (let i = 0; i < last; i++) { + if (isAnagrams(s, p, i)) { + res.push(i) + } + } + + return res +} + +let isAnagrams = function (s, p, start) { + let pl = p.length + let end = start + pl + + let sub = s.substring(start, end) + let subMap = {} + let pMap = {} + + countStr(sub, subMap) + countStr(p, pMap) + + let subKeys = Object.keys(subMap) + for (let subKey of subKeys) { + if (!pMap[subKey] || subMap[subKey] !== pMap[subKey]) { + return false + } + } + return true + + function countStr(str, map) { + for (let i = 0; i < str.length; i++) { + if (!map[str[i]]) { + map[str[i]] = 1 + } else { + map[str[i]]++ + } + } + } +} diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" new file mode 100644 index 0000000..84cbb59 --- /dev/null +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" @@ -0,0 +1,28 @@ +/** + * @param {string} s + * @return {number} + */ +let lengthOfLongestSubstring = function (str) { + let n = str.length + // 滑动窗口为s[left...right] + let left = 0 + let right = -1 + let freqMap = {} // 记录当前子串中下标对应的出现频率 + let max = 0 // 找到的满足条件子串的最长长度 + + while (left < n) { + let nextLetter = str[right + 1] + if (!freqMap[nextLetter] && nextLetter !== undefined) { + freqMap[nextLetter] = 1 + right++ + } else { + freqMap[str[left]] = 0 + left++ + } + max = Math.max(max, right - left + 1) + } + + return max +} + +console.log(lengthOfLongestSubstring("pwwkew")) \ No newline at end of file diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" new file mode 100644 index 0000000..cfee5ef --- /dev/null +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" @@ -0,0 +1,60 @@ +/** + * @param {string} s + * @param {string} t + * @return {string} + */ +let minWindow = function (s, t) { + // 先制定目标 根据t字符串统计出每个字符应该出现的个数 + let targetMap = makeCountMap(t) + + let sl = s.length + let left = 0 + let right = -1 + let countMap = {} + let min = "" + + while (left <= sl && right <= sl) { + let isValid = true + Object.keys(targetMap).forEach((key) => { + let targetCount = targetMap[key] + let count = countMap[key] + if (!count || count < targetCount) { + isValid = false + } + }) + + if (isValid) { + let currentValidLength = right - left + 1 + if (currentValidLength < min.length || min === "") { + min = s.substring(left, right + 1) + } + // 也要把map里对应的项去掉 + countMap[s[left]]-- + left++ + } else { + addCountToMap(countMap, s[right + 1]) + right++ + } + } + + return min +} + +function addCountToMap(map, str) { + if (!map[str]) { + map[str] = 1 + } else { + map[str]++ + } +} + +function makeCountMap(strs) { + let map = {} + for (let i = 0; i < strs.length; i++) { + let letter = strs[i] + addCountToMap(map, letter) + } + return map +} + +console.log(minWindow("ADOBECODEBANC", "ABC")) From 8c5fe4d8c0b95a7b1abf6e2e1b75970deb2b4c0f Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 30 May 2020 22:39:12 +0800 Subject: [PATCH 071/145] =?UTF-8?q?feat:=20=E6=9C=80=E5=B0=8F=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E5=AD=90=E4=B8=B2ac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- report.20200527.011151.36368.0.001.json | 443 ------------------ ...06\347\233\226\345\255\220\344\270\262.js" | 17 +- 2 files changed, 11 insertions(+), 449 deletions(-) delete mode 100644 report.20200527.011151.36368.0.001.json diff --git a/report.20200527.011151.36368.0.001.json b/report.20200527.011151.36368.0.001.json deleted file mode 100644 index 1257f6a..0000000 --- a/report.20200527.011151.36368.0.001.json +++ /dev/null @@ -1,443 +0,0 @@ - -{ - "header": { - "reportVersion": 1, - "event": "Allocation failed - JavaScript heap out of memory", - "trigger": "FatalError", - "filename": "report.20200527.011151.36368.0.001.json", - "dumpEventTime": "2020-05-27T01:11:51Z", - "dumpEventTimeStamp": "1590513111450", - "processId": 36368, - "cwd": "c:\\codes\\leetcode-javascript", - "commandLine": [ - "node", - "c:\\codes\\leetcode-javascript\\可被K整除的子数组.js" - ], - "nodejsVersion": "v12.13.1", - "wordSize": 64, - "arch": "x64", - "platform": "win32", - "componentVersions": { - "node": "12.13.1", - "v8": "7.7.299.13-node.16", - "uv": "1.33.1", - "zlib": "1.2.11", - "brotli": "1.0.7", - "ares": "1.15.0", - "modules": "72", - "nghttp2": "1.39.2", - "napi": "5", - "llhttp": "1.1.4", - "http_parser": "2.8.0", - "openssl": "1.1.1d", - "cldr": "35.1", - "icu": "64.2", - "tz": "2019c", - "unicode": "12.1" - }, - "release": { - "name": "node", - "lts": "Erbium", - "headersUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1-headers.tar.gz", - "sourceUrl": "https://nodejs.org/download/release/v12.13.1/node-v12.13.1.tar.gz", - "libUrl": "https://nodejs.org/download/release/v12.13.1/win-x64/node.lib" - }, - "osName": "Windows_NT", - "osRelease": "10.0.18362", - "osVersion": "Windows 10 Home China", - "osMachine": "x86_64", - "cpus": [ - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1276796, - "nice": 0, - "sys": 1216546, - "idle": 21543937, - "irq": 492109 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 584421, - "nice": 0, - "sys": 287875, - "idle": 23164468, - "irq": 26578 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 5303734, - "nice": 0, - "sys": 1111203, - "idle": 17621828, - "irq": 19171 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 959921, - "nice": 0, - "sys": 272703, - "idle": 22804140, - "irq": 4656 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 2716625, - "nice": 0, - "sys": 695484, - "idle": 20624656, - "irq": 16171 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 817859, - "nice": 0, - "sys": 212250, - "idle": 23006656, - "irq": 5984 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 2039062, - "nice": 0, - "sys": 662062, - "idle": 21335625, - "irq": 18093 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 681687, - "nice": 0, - "sys": 249515, - "idle": 23105546, - "irq": 5906 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1234406, - "nice": 0, - "sys": 382343, - "idle": 22420000, - "irq": 12562 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 833421, - "nice": 0, - "sys": 332765, - "idle": 22870546, - "irq": 10593 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 1193562, - "nice": 0, - "sys": 432562, - "idle": 22410625, - "irq": 15937 - }, - { - "model": "Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz", - "speed": 2592, - "user": 702109, - "nice": 0, - "sys": 308437, - "idle": 23026203, - "irq": 8187 - } - ], - "networkInterfaces": [ - { - "name": "Loopback Pseudo-Interface 1", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "::1", - "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", - "family": "IPv6", - "scopeid": 0 - }, - { - "name": "Loopback Pseudo-Interface 1", - "internal": true, - "mac": "00:00:00:00:00:00", - "address": "127.0.0.1", - "netmask": "255.0.0.0", - "family": "IPv4" - }, - { - "name": "以太网", - "internal": false, - "mac": "04:d4:c4:e1:d1:af", - "address": "192.168.125.124", - "netmask": "255.255.255.0", - "family": "IPv4" - } - ], - "host": "LAPTOP-FMHNK1K6" - }, - "javascriptStack": { - "message": "No stack.", - "stack": [ - "Unavailable." - ] - }, - "nativeStack": [ - { - "pc": "0x00007ff78cae1729", - "symbol": "std::basic_ostream >::operator<<+10873" - }, - { - "pc": "0x00007ff78cae5b4c", - "symbol": "std::basic_ostream >::operator<<+28316" - }, - { - "pc": "0x00007ff78cae4b08", - "symbol": "std::basic_ostream >::operator<<+24152" - }, - { - "pc": "0x00007ff78cbd369b", - "symbol": "v8::base::CPU::has_sse+37723" - }, - { - "pc": "0x00007ff78d3d82de", - "symbol": "v8::Isolate::ReportExternalAllocationLimitReached+94" - }, - { - "pc": "0x00007ff78d3c0321", - "symbol": "v8::SharedArrayBuffer::Externalize+833" - }, - { - "pc": "0x00007ff78d28dbec", - "symbol": "v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1436" - }, - { - "pc": "0x00007ff78d2a3c0d", - "symbol": "v8::internal::Factory::AllocateRawFixedArray+61" - }, - { - "pc": "0x00007ff78d2aafc4", - "symbol": "v8::internal::Factory::NewFixedArrayWithFiller+52" - }, - { - "pc": "0x00007ff78d2aaf81", - "symbol": "v8::internal::Factory::NewUninitializedFixedArray+65" - }, - { - "pc": "0x00007ff78d18c12f", - "symbol": "v8::Object::GetIsolate+7663" - }, - { - "pc": "0x00007ff78d03f13a", - "symbol": "v8::internal::OrderedHashMap::ValueAt+61274" - }, - { - "pc": "0x00007ff78d80404d", - "symbol": "v8::internal::SetupIsolateDelegate::SetupHeap+567949" - }, - { - "pc": "0x000001792d48493d", - "symbol": "" - } - ], - "javascriptHeap": { - "totalMemory": 939016192, - "totalCommittedMemory": 939016192, - "usedMemory": 907197656, - "availableMemory": 1289381792, - "memoryLimit": 2197815296, - "heapSpaces": { - "read_only_space": { - "memorySize": 262144, - "committedMemory": 262144, - "capacity": 261872, - "used": 32296, - "available": 229576 - }, - "new_space": { - "memorySize": 33554432, - "committedMemory": 33554432, - "capacity": 16759808, - "used": 3166504, - "available": 13593304 - }, - "old_space": { - "memorySize": 1945600, - "committedMemory": 1945600, - "capacity": 1151592, - "used": 1151592, - "available": 0 - }, - "code_space": { - "memorySize": 425984, - "committedMemory": 425984, - "capacity": 155232, - "used": 155232, - "available": 0 - }, - "map_space": { - "memorySize": 266240, - "committedMemory": 266240, - "capacity": 177600, - "used": 177600, - "available": 0 - }, - "large_object_space": { - "memorySize": 902512640, - "committedMemory": 902512640, - "capacity": 902510880, - "used": 902510880, - "available": 0 - }, - "code_large_object_space": { - "memorySize": 49152, - "committedMemory": 49152, - "capacity": 3552, - "used": 3552, - "available": 0 - }, - "new_large_object_space": { - "memorySize": 0, - "committedMemory": 0, - "capacity": 16759808, - "used": 0, - "available": 16759808 - } - } - }, - "resourceUsage": { - "userCpuSeconds": 3.875, - "kernelCpuSeconds": 1.187, - "cpuConsumptionPercent": 126.55, - "maxRss": 1558552576, - "pageFaults": { - "IORequired": 683613, - "IONotRequired": 0 - }, - "fsActivity": { - "reads": 2, - "writes": 88 - } - }, - "libuv": [ - ], - "environmentVariables": { - "=C:": "c:\\codes\\leetcode-javascript", - "ALLUSERSPROFILE": "C:\\ProgramData", - "AMD_ENTRYPOINT": "vs/workbench/services/extensions/node/extensionHostProcess", - "APPDATA": "C:\\Users\\94958\\AppData\\Roaming", - "APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "true", - "CommonProgramFiles": "C:\\Program Files\\Common Files", - "CommonProgramFiles(x86)": "C:\\Program Files (x86)\\Common Files", - "CommonProgramW6432": "C:\\Program Files\\Common Files", - "COMPUTERNAME": "LAPTOP-FMHNK1K6", - "ComSpec": "C:\\Windows\\system32\\cmd.exe", - "DriverData": "C:\\Windows\\System32\\Drivers\\DriverData", - "ELECTRON_RUN_AS_NODE": "1", - "GameFirst": "C:\\Program Files (x86)\\ASUS\\GameFirst\\", - "GameFirstUserPath": "C:\\Users\\94958\\AppData\\Local\\ASUS\\GameFirst", - "HOMEDRIVE": "C:", - "HOMEPATH": "\\Users\\94958", - "LOCALAPPDATA": "C:\\Users\\94958\\AppData\\Local", - "LOGONSERVER": "\\\\LAPTOP-FMHNK1K6", - "NUMBER_OF_PROCESSORS": "12", - "OneDrive": "C:\\Users\\94958\\OneDrive", - "OneDriveConsumer": "C:\\Users\\94958\\OneDrive", - "OS": "Windows_NT", - "Path": "C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\nodejs\\;C:\\Users\\94958\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\94958\\AppData\\Roaming\\npm;C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code\\bin", - "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC", - "PIPE_LOGGING": "true", - "PROCESSOR_ARCHITECTURE": "AMD64", - "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 158 Stepping 10, GenuineIntel", - "PROCESSOR_LEVEL": "6", - "PROCESSOR_REVISION": "9e0a", - "ProgramData": "C:\\ProgramData", - "ProgramFiles": "C:\\Program Files", - "ProgramFiles(x86)": "C:\\Program Files (x86)", - "ProgramW6432": "C:\\Program Files", - "PROMPT": "$P$G", - "PSModulePath": "C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules", - "PUBLIC": "C:\\Users\\Public", - "SESSIONNAME": "Console", - "SystemDrive": "C:", - "SystemRoot": "C:\\Windows", - "TEMP": "C:\\Users\\94958\\AppData\\Local\\Temp", - "TMP": "C:\\Users\\94958\\AppData\\Local\\Temp", - "USERDOMAIN": "LAPTOP-FMHNK1K6", - "USERDOMAIN_ROAMINGPROFILE": "LAPTOP-FMHNK1K6", - "USERNAME": "94958", - "USERPROFILE": "C:\\Users\\94958", - "VERBOSE_LOGGING": "true", - "VSCODE_CWD": "C:\\Users\\94958\\AppData\\Local\\Programs\\Microsoft VS Code", - "VSCODE_HANDLES_UNCAUGHT_ERRORS": "true", - "VSCODE_IPC_HOOK": "\\\\.\\pipe\\b8cbe1eb44c59330f23b7126ffc1d83e-1.45.1-main-sock", - "VSCODE_IPC_HOOK_EXTHOST": "\\\\.\\pipe\\vscode-ipc-31fc3b14-6b96-4af5-aa86-38e0a0760941-sock", - "VSCODE_LOGS": "C:\\Users\\94958\\AppData\\Roaming\\Code\\logs\\20200527T010954", - "VSCODE_LOG_STACK": "false", - "VSCODE_NLS_CONFIG": "{\"locale\":\"zh-cn\",\"availableLanguages\":{\"*\":\"zh-cn\"},\"_languagePackId\":\"2feefd28b9e8042903942873399b35a3.zh-cn\",\"_translationsConfigFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\\\\tcf.json\",\"_cacheRoot\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\",\"_resolvedLanguagePackCoreLocation\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\\\\5763d909d5f12fe19f215cbfdd29a91c0fa9208a\",\"_corruptedFile\":\"C:\\\\Users\\\\94958\\\\AppData\\\\Roaming\\\\Code\\\\clp\\\\2feefd28b9e8042903942873399b35a3.zh-cn\\\\corrupted.info\",\"_languagePackSupport\":true}", - "VSCODE_NODE_CACHED_DATA_DIR": "C:\\Users\\94958\\AppData\\Roaming\\Code\\CachedData\\5763d909d5f12fe19f215cbfdd29a91c0fa9208a", - "VSCODE_PID": "33348", - "windir": "C:\\Windows" - }, - "sharedObjects": [ - "C:\\Program Files\\nodejs\\node.exe", - "C:\\Windows\\SYSTEM32\\ntdll.dll", - "C:\\Windows\\System32\\KERNEL32.DLL", - "C:\\Windows\\System32\\KERNELBASE.dll", - "C:\\Windows\\System32\\WS2_32.dll", - "C:\\Windows\\System32\\RPCRT4.dll", - "C:\\Windows\\SYSTEM32\\dbghelp.dll", - "C:\\Windows\\System32\\ADVAPI32.dll", - "C:\\Windows\\System32\\ucrtbase.dll", - "C:\\Windows\\System32\\msvcrt.dll", - "C:\\Windows\\System32\\sechost.dll", - "C:\\Windows\\System32\\USER32.dll", - "C:\\Windows\\System32\\win32u.dll", - "C:\\Windows\\System32\\GDI32.dll", - "C:\\Windows\\System32\\gdi32full.dll", - "C:\\Windows\\System32\\msvcp_win.dll", - "C:\\Windows\\System32\\PSAPI.DLL", - "C:\\Windows\\System32\\CRYPT32.dll", - "C:\\Windows\\System32\\MSASN1.dll", - "C:\\Windows\\System32\\bcrypt.dll", - "C:\\Windows\\SYSTEM32\\USERENV.dll", - "C:\\Windows\\SYSTEM32\\IPHLPAPI.DLL", - "C:\\Windows\\System32\\profapi.dll", - "C:\\Windows\\SYSTEM32\\WINMM.dll", - "C:\\Windows\\SYSTEM32\\winmmbase.dll", - "C:\\Windows\\System32\\cfgmgr32.dll", - "C:\\Windows\\System32\\bcryptPrimitives.dll", - "C:\\Windows\\System32\\IMM32.DLL", - "C:\\Windows\\System32\\powrprof.dll", - "C:\\Windows\\System32\\UMPDC.dll", - "C:\\Windows\\SYSTEM32\\CRYPTBASE.DLL", - "C:\\Windows\\system32\\uxtheme.dll", - "C:\\Windows\\System32\\combase.dll", - "C:\\Windows\\SysWOW64\\ierd_tgp_lsp64.dll", - "C:\\Windows\\System32\\SHLWAPI.dll", - "C:\\Windows\\System32\\ole32.dll", - "C:\\Windows\\system32\\mswsock.dll", - "C:\\Windows\\system32\\wshqos.dll", - "C:\\Windows\\SYSTEM32\\wshtcpip.DLL", - "C:\\Windows\\SYSTEM32\\wship6.dll", - "C:\\Windows\\System32\\kernel.appcore.dll", - "C:\\Windows\\System32\\NSI.dll", - "C:\\Windows\\SYSTEM32\\dhcpcsvc6.DLL", - "C:\\Windows\\SYSTEM32\\dhcpcsvc.DLL", - "C:\\Windows\\SYSTEM32\\DNSAPI.dll", - "C:\\Windows\\system32\\napinsp.dll", - "C:\\Windows\\system32\\pnrpnsp.dll", - "C:\\Windows\\System32\\winrnr.dll", - "C:\\Windows\\system32\\NLAapi.dll", - "C:\\Windows\\system32\\wshbth.dll" - ] -} \ No newline at end of file diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" index cfee5ef..e855d8e 100644 --- "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\234\200\345\260\217\350\246\206\347\233\226\345\255\220\344\270\262.js" @@ -8,12 +8,15 @@ let minWindow = function (s, t) { let targetMap = makeCountMap(t) let sl = s.length - let left = 0 - let right = -1 - let countMap = {} - let min = "" + let tl = t.length + let left = 0 // 左边界 + let right = -1 // 右边界 + let countMap = {} // 当前窗口子串中 每个字符出现的次数 + let min = "" // 当前计算出的最小子串 - while (left <= sl && right <= sl) { + // 循环终止条件是两者有一者超出边界 + while (left <= sl - tl && right <= sl) { + // 和 targetMap 对比出现次数 确定是否满足条件 let isValid = true Object.keys(targetMap).forEach((key) => { let targetCount = targetMap[key] @@ -24,6 +27,7 @@ let minWindow = function (s, t) { }) if (isValid) { + // 如果满足 记录当前的子串 并且左边界右移 let currentValidLength = right - left + 1 if (currentValidLength < min.length || min === "") { min = s.substring(left, right + 1) @@ -32,6 +36,7 @@ let minWindow = function (s, t) { countMap[s[left]]-- left++ } else { + // 否则右边界右移 addCountToMap(countMap, s[right + 1]) right++ } @@ -57,4 +62,4 @@ function makeCountMap(strs) { return map } -console.log(minWindow("ADOBECODEBANC", "ABC")) +console.log(minWindow("aa", "a")) From a570292d60b8a87a772a1b5a71d12106d559a6e6 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 31 May 2020 00:12:42 +0800 Subject: [PATCH 072/145] =?UTF-8?q?feat:=20=E6=BB=91=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E6=9C=80=E5=A4=A7=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\274\202\344\275\215\350\257\215-438.js" | 64 +++++++++++-------- ...\235\242\350\257\225\351\242\23059 - I.js" | 24 +++++++ 2 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 "\346\273\221\345\212\250\347\252\227\345\217\243/\346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274-\351\235\242\350\257\225\351\242\23059 - I.js" diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" index 7394377..d1b1485 100644 --- "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" @@ -4,46 +4,56 @@ * @return {number[]} */ let findAnagrams = function (s, p) { + let targetMap = makeCountMap(p) let sl = s.length let pl = p.length - let last = sl - pl + 1 - + // [left,...right] 滑动窗口 + let left = 0 + let right = pl - 1 + let windowMap = makeCountMap(s.substring(left, right + 1)) let res = [] - for (let i = 0; i < last; i++) { - if (isAnagrams(s, p, i)) { - res.push(i) + + while (left <= sl - pl && right < sl) { + if (isAnagrams(windowMap, targetMap)) { + res.push(left) } + windowMap[s[left]]-- + right++ + left++ + addCountToMap(windowMap, s[right]) } return res } -let isAnagrams = function (s, p, start) { - let pl = p.length - let end = start + pl - - let sub = s.substring(start, end) - let subMap = {} - let pMap = {} - - countStr(sub, subMap) - countStr(p, pMap) - - let subKeys = Object.keys(subMap) - for (let subKey of subKeys) { - if (!pMap[subKey] || subMap[subKey] !== pMap[subKey]) { +let isAnagrams = function (windowMap, targetMap) { + let targetKeys = Object.keys(targetMap) + for (let targetKey of targetKeys) { + if ( + !windowMap[targetKey] || + windowMap[targetKey] !== targetMap[targetKey] + ) { return false } } return true +} - function countStr(str, map) { - for (let i = 0; i < str.length; i++) { - if (!map[str[i]]) { - map[str[i]] = 1 - } else { - map[str[i]]++ - } - } +function addCountToMap(map, str) { + if (!map[str]) { + map[str] = 1 + } else { + map[str]++ + } +} + +function makeCountMap(strs) { + let map = {} + for (let i = 0; i < strs.length; i++) { + let letter = strs[i] + addCountToMap(map, letter) } + return map } + +console.log(findAnagrams("abab", "ab")) diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274-\351\235\242\350\257\225\351\242\23059 - I.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274-\351\235\242\350\257\225\351\242\23059 - I.js" new file mode 100644 index 0000000..f2c51cf --- /dev/null +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274-\351\235\242\350\257\225\351\242\23059 - I.js" @@ -0,0 +1,24 @@ +let maxSlidingWindow = function (nums, k) { + if (k === 0 || !nums.length) { + return [] + } + let left = 0 + let right = k - 1 + let res = [findMax(nums, left, right)] + + while (right < nums.length - 1) { + right++ + left++ + res.push(findMax(nums, left, right)) + } + + return res +} + +function findMax(nums, left, right) { + let max = -Infinity + for (let i = left; i <= right; i++) { + max = Math.max(max, nums[i]) + } + return max +} \ No newline at end of file From 9179a10054546c23ab7ccd8c24ccddadda9065df Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 31 May 2020 00:15:17 +0800 Subject: [PATCH 073/145] =?UTF-8?q?refactor:=20=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...204\345\261\202\346\254\241\351\201\215\345\216\206 II-107.js" | 0 ...270\252\346\234\211\345\272\217\346\225\260\347\273\204-88.js" | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename "\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" => "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II-107.js" (100%) rename "\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" => "\345\217\214\346\214\207\351\222\210/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204-88.js" (100%) diff --git "a/\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II-107.js" similarity index 100% rename from "\344\272\214\345\217\211\346\240\221/107. \344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II.js" rename to "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\261\202\346\254\241\351\201\215\345\216\206 II-107.js" diff --git "a/\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" "b/\345\217\214\346\214\207\351\222\210/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204-88.js" similarity index 100% rename from "\345\217\214\346\214\207\351\222\210/88.\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.js" rename to "\345\217\214\346\214\207\351\222\210/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204-88.js" From 2a0002771214c94f000830a6a114315fd760546c Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 31 May 2020 13:28:27 +0800 Subject: [PATCH 074/145] =?UTF-8?q?feat:=20=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\351\223\276\350\241\250.js" | 21 +++++++- ...347\232\204\346\213\254\345\217\267-20.js" | 52 +++++++++++++++++++ ...344\270\252\350\212\202\347\202\271-19.js" | 41 +++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 "\346\240\210\345\222\214\351\230\237\345\210\227/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-20.js" create mode 100644 "\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271-19.js" diff --git "a/\345\267\245\345\205\267/\351\223\276\350\241\250.js" "b/\345\267\245\345\205\267/\351\223\276\350\241\250.js" index 53cc2ab..7479607 100644 --- "a/\345\267\245\345\205\267/\351\223\276\350\241\250.js" +++ "b/\345\267\245\345\205\267/\351\223\276\350\241\250.js" @@ -3,4 +3,23 @@ function ListNode(val) { this.next = null } -module.exports = ListNode \ No newline at end of file +/** + * 数组 -> 链表 + * @param {number[]} vals + */ +function makeListNode(vals) { + let head = new ListNode(vals[0]) + let i = 1 + let cur = head + while (i < vals.length) { + let val = vals[i] + cur.next = new ListNode(val) + cur = cur.next + i++ + } + return head +} + +module.exports = ListNode + +module.exports.makeListNode = makeListNode diff --git "a/\346\240\210\345\222\214\351\230\237\345\210\227/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-20.js" "b/\346\240\210\345\222\214\351\230\237\345\210\227/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-20.js" new file mode 100644 index 0000000..b02a587 --- /dev/null +++ "b/\346\240\210\345\222\214\351\230\237\345\210\227/\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267-20.js" @@ -0,0 +1,52 @@ +/** + * @param {string} s + * @return {boolean} + */ +let isValid = function (s) { + let sl = s.length + if (sl % 2 !== 0) return false + let leftToRight = { + "{": "}", + "[": "]", + "(": ")", + } + // 建立一个反向的 value -> key 映射表 + let rightToLeft = createReversedMap(leftToRight) + // 用来匹配左右括号的栈 + let stack = [] + + for (let i = 0; i < s.length; i++) { + let bracket = s[i] + // 左括号 放进栈中 + if (leftToRight[bracket]) { + stack.push(bracket) + } else { + let needLeftBracket = rightToLeft[bracket] + // 左右括号都不是 直接失败 + if (!needLeftBracket) { + return false + } + + // 栈中取出最后一个括号 如果不是需要的那个左括号 就失败 + let lastBracket = stack.pop() + if (needLeftBracket !== lastBracket) { + return false + } + } + } + + if (stack.length) { + return false + } + return true +} + +function createReversedMap(map) { + return Object.keys(map).reduce((prev, key) => { + const value = map[key] + prev[value] = key + return prev + }, {}) +} + +console.log(isValid('({})[()]')) \ No newline at end of file diff --git "a/\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271-19.js" "b/\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271-19.js" new file mode 100644 index 0000000..3fa6ae6 --- /dev/null +++ "b/\351\223\276\350\241\250/\345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\345\200\222\346\225\260\347\254\254N\344\270\252\350\212\202\347\202\271-19.js" @@ -0,0 +1,41 @@ +const { makeListNode } = require('../工具/链表') +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} n + * @return {ListNode} + */ +let removeNthFromEnd = function (head, n) { + let node = head + let nodes = [] + do { + nodes.push(node) + node = node.next + } while (node) + + const l = nodes.length + const index = l - n + const targetNode = nodes[index] + const prevNode = nodes[index - 1] + if (prevNode) { + prevNode.next = targetNode.next + } + + const tempNext = targetNode.next + targetNode.next = null + + + if (targetNode === head) { + return tempNext + } + return head +}; + +const node = makeListNode([1, 2, 3, 4, 5]) +console.log(removeNthFromEnd(node, 5)) \ No newline at end of file From 6a21b30cf8918ebd493b32015bda16115bb86aa1 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 31 May 2020 18:12:49 +0800 Subject: [PATCH 075/145] =?UTF-8?q?feat:=20=E9=80=86=E6=B3=A2=E5=85=B0?= =?UTF-8?q?=E7=9F=A9=E9=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\274\217\346\261\202\345\200\274-150.js" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "\346\240\210\345\222\214\351\230\237\345\210\227/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274-150.js" diff --git "a/\346\240\210\345\222\214\351\230\237\345\210\227/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274-150.js" "b/\346\240\210\345\222\214\351\230\237\345\210\227/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274-150.js" new file mode 100644 index 0000000..5b9fcec --- /dev/null +++ "b/\346\240\210\345\222\214\351\230\237\345\210\227/\351\200\206\346\263\242\345\205\260\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274-150.js" @@ -0,0 +1,28 @@ +/** + * @param {string[]} tokens + * @return {number} + */ +let opMap = { + "+": (a, b) => b + a, + "-": (a, b) => b - a, + "*": (a, b) => b * a, + "/": (a, b) => parseInt(b / a, 10), +} + +let evalRPN = function (tokens) { + let stack = [] + for (let token of tokens) { + let op = opMap[token] + if (op) { + let a = parseInt(stack.pop()) + let b = parseInt(stack.pop()) + let res = op(a, b) + stack.push(res) + } else { + stack.push(token) + } + } + return stack[0] +} + +console.log(evalRPN(["-8","23","8","-","9","23","-","-","*","33","-8","/","+","38","-14","-","-","-7","32","-19","-","11","+","+","+","14","22","-","-","27","-9","-","+","31","+","-12","-11","-","-","14","+","30","+","37","30","-","+","-9","+","7","-","37","+","-5","13","/","-","19","-2","-19","12","+","-","23","+","-","-19","-","+","6","+","-17","+","17","+","5","36","+","-10","+","+","23","-8","-","-","18","-","31","-16","-","+","34","+","-6","+","24","-","22","-","-8","-","28","+","-12","+","39","28","-7","+","+","-14","5","+","5","+","10","+","+","+","-18","*","10","+","-5","11","-","6","+","-","-12","31","+","+","30","29","-","-","39","+","13","-8","-5","+","-","26","19","-","*","-","10","-","-20","5","+","+","0","-","28","-","19","/","28","+","-18","-","28","20","+","-5","-19","+","+","-","-12","-","3","-","6","-15","+","4","-","-","38","+","-9","-","38","-","12","-20","-","10","5","-15","-","-","-","+","-11","+","5","+","2","-","28","+","-9","-11","-","+","37","-","-17","31","-","2","+","+","-16","-12","-","-","12","+","34","-","15","+","8","+","17","-","2","-","33","+","-5","+","14","+","29","-","33","23","+","26","30","-","+","+","39","+","9","24","-","-","20","15","+","-","24","+","37","-","30","-1","-","+","34","+","-13","-","23","15","-","-","-5","-8","8","30","35","-9","22","+","-","-","36","-1","+","5","-","-","+","25","-","+","27","-","16","+","+","+","39","-","15","-","-3","+","5","-6","-","+","-6","-15","-7","-","+","/","13","-","18","+","4","+","29","+","-17","0","-6","-20","-17","+","12","-","+","-","+","+","-10","22","+","+","-11","-","-2","38","-","-","-6","+","0","-","-10","+","-4","-10","+","-","0","-","31","30","-","37","5","+","+","+","-15","+","38","4","-","-16","-17","+","+","+","38","-","27","-19","/","12","+","/"])) From b03ed0c6a28c5b911fa3ef5ad910612bb2398b16 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 31 May 2020 18:51:18 +0800 Subject: [PATCH 076/145] =?UTF-8?q?feat:=20=E7=AE=80=E5=8C=96=E8=B7=AF?= =?UTF-8?q?=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...22\345\272\217\351\200\237\345\272\246.js" | 2 +- ...22\346\263\241\346\216\222\345\272\217.js" | 2 +- ...2\345\272\217-\344\270\211\350\267\257.js" | 19 ++++++++++- ...2\345\272\217-\347\251\272\351\227\264.js" | 2 +- ...53\351\200\237\346\216\222\345\272\217.js" | 2 +- ...11\346\213\251\346\216\222\345\272\217.js" | 2 +- ...345\214\226\350\267\257\345\276\204-71.js" | 33 +++++++++++++++++++ 7 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 "\346\240\210\345\222\214\351\230\237\345\210\227/\347\256\200\345\214\226\350\267\257\345\276\204-71.js" diff --git "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" index 0b4e20a..9fc27af 100644 --- "a/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" +++ "b/\345\267\245\345\205\267/\346\216\222\345\272\217\351\200\237\345\272\246.js" @@ -87,5 +87,5 @@ glob("排序/*.js", (err, result) => { .filter(Boolean) sortTest(sortFunctions, () => getRandomArray(10000), "普通数组排序") sortTest(sortFunctions, () => getNearlyArray(10000), "近似数组排序") - sortTest(sortFunctions, () => getRangedArray(10000), "大量重复值元素排序") + sortTest(sortFunctions, () => getRangedArray(500), "大量重复值元素排序") }) diff --git "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" index 6293e20..11e1213 100644 --- "a/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\206\222\346\263\241\346\216\222\345\272\217.js" @@ -26,4 +26,4 @@ function bubbleSort(arr) { bubbleSort.sortName = "冒泡排序" -module.exports = bubbleSort +// module.exports = bubbleSort diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" index c77baf8..cab4058 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\344\270\211\350\267\257.js" @@ -43,7 +43,24 @@ function partition(arr, left, right) { // 三路 注意看注释里的区间 let lt = left // arr[left + 1...lt] < v let gt = right + 1 // arr[gt...r] > v - let index = left + 1 // arr[lt + 1...index] === v + let index = left + 1 // arr[lt + 1...index) === v + + while (index < gt) { + let num = arr[index] + if (num < pivot) { + swap(arr, index, lt + 1) + lt++ + index++ + } else if (num > pivot) { + swap(arr, index, gt - 1) + gt-- + } else if (num === pivot) { + index++ + } + } + swap(arr, left, lt) + + return [lt - 1, gt] } quickSort.sortName = "快速排序(三路)" diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" index 023fcad..6777d4a 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217-\347\251\272\351\227\264.js" @@ -21,4 +21,4 @@ function quickSort(arr) { quickSort.sortName = "快速排序(空间版)" -module.exports = quickSort +// module.exports = quickSort diff --git "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" index b41a667..a6bd467 100644 --- "a/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\345\277\253\351\200\237\346\216\222\345\272\217.js" @@ -56,4 +56,4 @@ function partition(arr, left, right) { quickSort.sortName = "快速排序" -module.exports = quickSort +// module.exports = quickSort diff --git "a/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" "b/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" index c51d86b..54e1ce7 100644 --- "a/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" +++ "b/\346\216\222\345\272\217/\351\200\211\346\213\251\346\216\222\345\272\217.js" @@ -15,4 +15,4 @@ function selectSort(arr) { selectSort.sortName = "选择排序" -module.exports = selectSort +// module.exports = selectSort diff --git "a/\346\240\210\345\222\214\351\230\237\345\210\227/\347\256\200\345\214\226\350\267\257\345\276\204-71.js" "b/\346\240\210\345\222\214\351\230\237\345\210\227/\347\256\200\345\214\226\350\267\257\345\276\204-71.js" new file mode 100644 index 0000000..caed5c7 --- /dev/null +++ "b/\346\240\210\345\222\214\351\230\237\345\210\227/\347\256\200\345\214\226\350\267\257\345\276\204-71.js" @@ -0,0 +1,33 @@ +/** + * @param {string} path + * @return {string} + */ +let simplifyPath = function (path) { + let tokens = path.split("/") + let stack = [] + + for (let index = 0; index < tokens.length; index++) { + let token = tokens[index] + if (token === "..") { + if (stack.length > 0) { + stack.pop() + } + } else if (!(token === '') && !(token === '.')) { + stack.push(token) + } + } + + let res = '/' + + for (let token of stack) { + res += `${token}/` + } + + if (res !== '/') { + res = res.substr(0, res.length - 1) + } + + return res +} + +console.log(simplifyPath("/home/")) From 73e6b319f8b2e6627474d8a7b7ad586abc38213e Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 1 Jun 2020 00:54:42 +0800 Subject: [PATCH 077/145] =?UTF-8?q?feat:=20=E4=BB=8E=E4=B8=8A=E5=88=B0?= =?UTF-8?q?=E4=B8=8B=E6=89=93=E5=8D=B0=E4=BA=8C=E5=8F=89=E6=A0=91=20III?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\272\214\345\217\211\346\240\221.js" | 10 ++-- ...45\272\217\351\201\215\345\216\206-144.js" | 47 +++++++++++++++++++ ...\351\235\242\350\257\225\351\242\23032.js" | 39 +++++++++++++++ 3 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 "\346\240\210\345\222\214\351\230\237\345\210\227/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206-144.js" create mode 100644 "\346\240\210\345\222\214\351\230\237\345\210\227/\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III-\351\235\242\350\257\225\351\242\23032.js" diff --git "a/\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" "b/\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" index adf1c96..6664215 100644 --- "a/\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" +++ "b/\345\267\245\345\205\267/\344\272\214\345\217\211\346\240\221.js" @@ -1,9 +1,9 @@ class TreeNode { - constructor(val) { - this.val = val - this.left = null - this.right = null - } + constructor(val) { + this.val = val + this.left = null + this.right = null + } } module.exports = TreeNode \ No newline at end of file diff --git "a/\346\240\210\345\222\214\351\230\237\345\210\227/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206-144.js" "b/\346\240\210\345\222\214\351\230\237\345\210\227/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206-144.js" new file mode 100644 index 0000000..ee7bca5 --- /dev/null +++ "b/\346\240\210\345\222\214\351\230\237\345\210\227/\344\272\214\345\217\211\346\240\221\347\232\204\345\211\215\345\272\217\351\201\215\345\216\206-144.js" @@ -0,0 +1,47 @@ +const TreeNode = require("../工具/二叉树") + +/** + * @param {TreeNode} root + * @return {number[]} + */ +let preorderTraversal = function (root) { + let res = [] + let stack = [ + { + type: "go", + node: root, + }, + ] + + while (stack.length) { + let { type, node } = stack.pop() + + if (!node) continue + + if (type === "print") { + res.push(node.val) + } + + if (type === "go") { + stack.push({ type: "print", node }) + + if (node.right) { + stack.push({ type: "go", node: node.right }) + } + + if (node.left) { + stack.push({ type: "go", node: node.left }) + } + } + } + + return res +} + +const tree = new TreeNode(1) +tree.left = new TreeNode(4) +tree.left.left = new TreeNode(5) +tree.right = new TreeNode(6) +tree.right.right = new TreeNode(7) + +console.log(preorderTraversal(tree)) diff --git "a/\346\240\210\345\222\214\351\230\237\345\210\227/\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III-\351\235\242\350\257\225\351\242\23032.js" "b/\346\240\210\345\222\214\351\230\237\345\210\227/\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III-\351\235\242\350\257\225\351\242\23032.js" new file mode 100644 index 0000000..62b4b41 --- /dev/null +++ "b/\346\240\210\345\222\214\351\230\237\345\210\227/\344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221III-\351\235\242\350\257\225\351\242\23032.js" @@ -0,0 +1,39 @@ +const TreeNode = require("../工具/二叉树") + +let levelOrder = function (root) { + let queue = [root] + let res = [] + if (!root) return res + let level = 0 + while (queue.length) { + level++ + let subRes = [] + let len = queue.length + let shouldReverse = level % 2 === 0 + + for (let i = 0; i < len; i++) { + let node = queue.shift() + subRes.push(node.val) + if (node.left) { + queue.push(node.left) + } + if (node.right) { + queue.push(node.right) + } + } + // 偶数行 把结果子数组reverse即可 + if (shouldReverse) { + subRes.reverse() + } + res.push(subRes) + } + return res +} + +let tree = new TreeNode(1) +tree.left = new TreeNode(2) +tree.left.left = new TreeNode(4) +tree.right = new TreeNode(3) +tree.right.right = new TreeNode(5) + +console.log(levelOrder(tree)) From f15e5312ef489f570bd48cc95a0712a11a558d20 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 1 Jun 2020 02:12:59 +0800 Subject: [PATCH 078/145] =?UTF-8?q?feat:=20=E4=B8=A4=E4=B8=A4=E4=BA=A4?= =?UTF-8?q?=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\232\204\350\212\202\347\202\271-24.js" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" diff --git "a/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" "b/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" new file mode 100644 index 0000000..527b48a --- /dev/null +++ "b/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" @@ -0,0 +1,28 @@ +const ListNode = require("../工具/链表") + +let swapPairs = function (head) { + let helper = function (node) { + let tempNext = node.next + if (tempNext) { + let tempNextNext = node.next.next + node.next.next = node + if (tempNextNext) { + node.next = helper(tempNextNext) + }else { + node.next = null + } + } + return tempNext + } + + let res = helper(head) + + return res +} + +let node = new ListNode(1) +node.next = new ListNode(2) +node.next.next = new ListNode(3) +node.next.next.next = new ListNode(4) + +console.log(swapPairs(node)) From 88a5b2f0fd53425649d6026f9dcf5ecce7070286 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 5 Jun 2020 00:03:40 +0800 Subject: [PATCH 079/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=8F=B3=E8=A7=86=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\217\263\350\247\206\345\233\276-199.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276-199.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276-199.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276-199.js" new file mode 100644 index 0000000..5ef8278 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\350\247\206\345\233\276-199.js" @@ -0,0 +1,23 @@ +let rightSideView = function (root) { + if (!root) return [] + let queue = [root] + let res = [] + while (queue.length) { + let len = queue.length + let last + for (let i = 0; i < len; i++) { + let node = queue.shift() + if (node.left) { + queue.push(node.left) + } + if (node.right) { + queue.push(node.right) + } + if (node.val !== undefined) { + last = node.val + } + } + res.push(last) + } + return res +} From 81cad5f646f8959df32ff6ae9d2b6debb327e94e Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 5 Jun 2020 10:39:57 +0800 Subject: [PATCH 080/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\244\247\346\267\261\345\272\246-104.js" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246-104.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246-104.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246-104.js" new file mode 100644 index 0000000..285021b --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\244\247\346\267\261\345\272\246-104.js" @@ -0,0 +1,35 @@ +/* + * @lc app=leetcode id=104 lang=javascript + * + * [104] Maximum Depth of Binary Tree + */ + +// @lc code=start +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +let maxDepth = function (root) { + let max = 0 + let helper = (node, depth) => { + if (!node) return + max = Math.max(max, depth) + if (node.left) { + helper(node.left, depth + 1) + } + if (node.right) { + helper(node.right, depth + 1) + } + } + helper(root, 1) + return max +} +// @lc code=end From 160ec6dc9ecb808c29452bfcb00eb9ddf3ddea97 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 6 Jun 2020 04:22:00 +0800 Subject: [PATCH 081/145] =?UTF-8?q?feat:=20=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\272\214\345\217\211\346\240\221-101.js" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221-101.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221-101.js" "b/\344\272\214\345\217\211\346\240\221/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221-101.js" new file mode 100644 index 0000000..cfef11c --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\345\257\271\347\247\260\344\272\214\345\217\211\346\240\221-101.js" @@ -0,0 +1,28 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isSymmetric = function (root) { + if (!root) return true + let helper = (left, right) => { + if (!left && !right) { + return true + } + if (!left || !right) { + return false + } + if (left.val === right.val) { + return helper(left.left, right.right) && helper(left.right, right.left) + } else { + return false + } + } + return helper(root, root) +} From 13a58ee17103684ebcdb19889bcd986a36ee9ac4 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 6 Jun 2020 21:34:38 +0800 Subject: [PATCH 082/145] =?UTF-8?q?feat:=20=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\344\272\214\345\217\211\346\240\221-110.js" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221-110.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221-110.js" "b/\344\272\214\345\217\211\346\240\221/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221-110.js" new file mode 100644 index 0000000..9fb5be1 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\345\271\263\350\241\241\344\272\214\345\217\211\346\240\221-110.js" @@ -0,0 +1,15 @@ +let isBalanced = function (root) { + if (!root) { + return true + } + + let isSonBalnaced = + Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1 + + return isSonBalnaced && isBalanced(root.left) && isBalanced(root.right) +} + +function getHeight(node) { + if (!node) return 0 + return Math.max(getHeight(node.left), getHeight(node.right)) + 1 +} From cb4075a9be05140cb38661085cba11710dfaf227 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 7 Jun 2020 15:20:39 +0800 Subject: [PATCH 083/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\234\211\350\267\257\345\276\204-257.js" | 18 +++++++++++++++ ...45\255\220\344\271\213\345\222\214-404.js" | 22 +++++++++++++++++++ ...45\276\204\346\200\273\345\222\214-112.js" | 14 ++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204-257.js" create mode 100644 "\344\272\214\345\217\211\346\240\221/\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214-404.js" create mode 100644 "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214-112.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204-257.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204-257.js" new file mode 100644 index 0000000..9df9927 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\211\200\346\234\211\350\267\257\345\276\204-257.js" @@ -0,0 +1,18 @@ +let binaryTreePaths = function (root) { + let res = [] + let dfs = (node, path = "") => { + if (!node) { + return + } + + let newPath = path ? `${path}->${node.val}` : `${node.val}` + if (!node.left && !node.right) { + res.push(newPath) + } + + dfs(node.left, newPath) + dfs(node.right, newPath) + } + dfs(root) + return res +} diff --git "a/\344\272\214\345\217\211\346\240\221/\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214-404.js" "b/\344\272\214\345\217\211\346\240\221/\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214-404.js" new file mode 100644 index 0000000..72f1a56 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\345\267\246\345\217\266\345\255\220\344\271\213\345\222\214-404.js" @@ -0,0 +1,22 @@ +let sumOfLeftLeaves = function (root) { + let sum = 0 + + let dfs = (node) => { + if (!node) return + + if (isLeaf(node.left)) { + sum += node.left.val + } + + dfs(node.left) + dfs(node.right) + } + + dfs(root) + + return sum +} + +function isLeaf(node) { + return !!node && !node.left && !node.right +} diff --git "a/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214-112.js" "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214-112.js" new file mode 100644 index 0000000..753b265 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214-112.js" @@ -0,0 +1,14 @@ +let hasPathSum = function (root, sum) { + if (!root) { + return false + } + // 叶子节点 判断当前的值是否等于 sum 即可 + if (!root.left && !root.right) { + return root.val === sum + } + + return ( + hasPathSum(root.left, sum - root.val) || + hasPathSum(root.right, sum - root.val) + ) +} From 03a19c84f6c9056fd05fc735a162c5022776d79f Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 7 Jun 2020 17:00:25 +0800 Subject: [PATCH 084/145] =?UTF-8?q?feat:=20=E6=B1=82=E6=A0=B9=E5=88=B0?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E8=8A=82=E7=82=B9=E6=95=B0=E5=AD=97=E4=B9=8B?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\255\227\344\271\213\345\222\214-129.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214-129.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214-129.js" "b/\344\272\214\345\217\211\346\240\221/\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214-129.js" new file mode 100644 index 0000000..9695144 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\346\261\202\346\240\271\345\210\260\345\217\266\345\255\220\350\212\202\347\202\271\346\225\260\345\255\227\344\271\213\345\222\214-129.js" @@ -0,0 +1,23 @@ +let sumNumbers = function (root) { + let paths = [] + + let dfs = (node, path) => { + if (!node) { + return + } + + let newPath = `${path}${node.val}` + if (!node.left && !node.right) { + paths.push(newPath) + return + } + + dfs(node.left, newPath) + dfs(node.right, newPath) + } + + dfs(root, "") + return paths.reduce((total, val) => { + return total + Number(val) + }, 0) +} From 10f11e10edd6b464f2ff02671c279c88b93fdd3f Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 7 Jun 2020 18:32:59 +0800 Subject: [PATCH 085/145] =?UTF-8?q?feat:=20=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C=20III?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76\204\346\200\273\345\222\214 III-437.js" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 III-437.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 III-437.js" "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 III-437.js" new file mode 100644 index 0000000..40faec2 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 III-437.js" @@ -0,0 +1,22 @@ +let pathSum = function (root, sum) { + if (!root) return 0 + return ( + countSum(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum) + ) +} + +let countSum = (node, sum) => { + let count = 0 + let dfs = (node, target) => { + if (!node) return + + if (node.val === target) { + count += 1 + } + + dfs(node.left, target - node.val) + dfs(node.right, target - node.val) + } + dfs(node, sum) + return count +} From 54f5edff3528cd38ab63a362558dbb5d3210671c Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 7 Jun 2020 18:41:53 +0800 Subject: [PATCH 086/145] fix: typo --- .../\350\267\257\345\276\204\346\200\273\345\222\214 II-113.js" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" => "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 II-113.js" (100%) diff --git "a/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" "b/\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 II-113.js" similarity index 100% rename from "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\220\210II-113.js" rename to "\344\272\214\345\217\211\346\240\221/\350\267\257\345\276\204\346\200\273\345\222\214 II-113.js" From 1da09c9a0dad5b553fe5d5edad7cdb045647a176 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 9 Jun 2020 15:00:44 +0800 Subject: [PATCH 087/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\260\217\346\267\261\345\272\246-111.js" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246-111.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246-111.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246-111.js" new file mode 100644 index 0000000..58ee699 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\345\260\217\346\267\261\345\272\246-111.js" @@ -0,0 +1,27 @@ +let minDepth = function (root) { + if (!root) return 0 + + let depth = 0 + let queue = [root] + + while (queue.length) { + depth++ + let len = queue.length + while (len--) { + let node = queue.shift() + + let left = node.left + let right = node.right + if (!left && !right) { + return depth + } + + if (left) { + queue.push(left) + } + if (right) { + queue.push(right) + } + } + } +} From 9d0043988e5a7d830313c4f5b3127f18c679d7b4 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 9 Jun 2020 15:47:11 +0800 Subject: [PATCH 088/145] =?UTF-8?q?feat:=20=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\232\204\350\212\202\347\202\271-450.js" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271-450.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271-450.js" "b/\344\272\214\345\217\211\346\240\221/\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271-450.js" new file mode 100644 index 0000000..8f96629 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\345\210\240\351\231\244\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\350\212\202\347\202\271-450.js" @@ -0,0 +1,52 @@ +let deleteNode = function (root, key) { + let findNodePos = (node, key) => { + if (!node) { + return false + } + if (node.left && node.left.val === key) { + return { + parent: node, + pos: "left", + } + } else if (node.right && node.right.val === key) { + return { + parent: node, + pos: "right", + } + } else { + return findNodePos(node.left, key) || findNodePos(node.right, key) + } + } + + let findLastLeft = (node) => { + if (!node.left) { + return node + } + return findLastLeft(node.left) + } + + let virtual = new TreeNode() + virtual.left = root + + let finded = findNodePos(virtual, key) + if (finded) { + let { parent, pos } = finded + let target = parent[pos] + let targetLeft = target.left + let targetRight = target.right + + if (!targetLeft && !targetRight) { + parent[pos] = null + } else if (!targetRight) { + parent[pos] = targetLeft + } else if (!targetLeft) { + parent[pos] = targetRight + } else { + parent[pos] = targetRight + let lastLeft = findLastLeft(targetRight) + lastLeft.left = targetLeft + } + } + + return virtual.left +} \ No newline at end of file From 34435b62dd237d7ce6770b4d90a6562e31d7842c Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Tue, 9 Jun 2020 17:36:22 +0800 Subject: [PATCH 089/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=AB=A0=E8=8A=82=E7=BB=93=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...17\347\232\204\345\205\203\347\264\240.js" | 34 +++++++++++++++++++ ...11\346\220\234\347\264\242\346\240\221.js" | 13 +++++++ 2 files changed, 47 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.js" create mode 100644 "\344\272\214\345\217\211\346\240\221/\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.js" new file mode 100644 index 0000000..30d1ff5 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240.js" @@ -0,0 +1,34 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthSmallest = function (root, k) { + let count = 0 + let finded + + let dfs = (node) => { + if (!node) { + return + } + dfs(node.left) + count++ + if (count === k) { + finded = node.val + return + } + dfs(node.right) + } + + dfs(root) + + return finded +} diff --git "a/\344\272\214\345\217\211\346\240\221/\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.js" "b/\344\272\214\345\217\211\346\240\221/\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.js" new file mode 100644 index 0000000..8f717c4 --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\345\260\206\346\234\211\345\272\217\346\225\260\347\273\204\350\275\254\344\270\272\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221.js" @@ -0,0 +1,13 @@ +let sortedArrayToBST = function (nums) { + let n = nums.length + if (!n) { + return null + } + let mid = Math.floor(n / 2) + let root = new TreeNode(nums[mid]) + + root.left = sortedArrayToBST(nums.slice(0, mid)) + root.right = sortedArrayToBST(nums.slice(mid + 1, n)) + + return root +}; \ No newline at end of file From b462f6c984edf09a5190a3dff0dd17143c686252 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Wed, 10 Jun 2020 19:15:03 +0800 Subject: [PATCH 090/145] =?UTF-8?q?feat:=20=E7=94=B5=E8=AF=9D=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\257\215\347\273\204\345\220\210-17.js" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" diff --git "a/DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" "b/DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" new file mode 100644 index 0000000..57bda1a --- /dev/null +++ "b/DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" @@ -0,0 +1,48 @@ +/** + * @param {string} digits + * @return {string[]} + */ +let letterMap = [ + " ", //0 + "", //1 + "abc", //2 + "def", //3 + "ghi", //4 + "jkl", //5 + "mno", //6 + "pqrs", //7 + "tuv", //8 + "wxyz", //9 +] + +let letterCombinations = function (digits) { + let res = [] + + if (digits === "") { + return res + } + + /** + * + * @param {number} index 当前处理到的下标位置 + * @param {string} str 当前已经凑成的字符串 + */ + let findCombinations = (index, str) => { + if (digits.length === index) { + res.push(str) + return + } + + let char = digits[index] // 数字 + let letters = letterMap[Number(char)] // 数字对应的字母 + + for (let i = 0; i < letters.length; i++) { + let letter = letters[i] + findCombinations(index + 1, `${str}${letter}`) + } + } + + findCombinations(0, "") + + return res +} From 5aef9dc6a61fae1e1f26d9d3c43c5117f970d1b1 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 11 Jun 2020 01:27:20 +0800 Subject: [PATCH 091/145] =?UTF-8?q?feat:=20=E5=A4=8D=E5=8E=9Fip=E5=9C=B0?= =?UTF-8?q?=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\216\237ip\345\234\260\345\235\200-93.js" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" diff --git "a/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" "b/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" new file mode 100644 index 0000000..88945e5 --- /dev/null +++ "b/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" @@ -0,0 +1,43 @@ +/** + * @param {string} s + * @return {string[]} + */ +let restoreIpAddresses = function (s) { + let res = [] + let findPos = (start, prev, used) => { + if (used === 3) { + // 点全部用光后 剩余字符的长度不能超过3 就是一个答案 + let rest = s.substr(start) + // 最后一位不能为0 且长度在 (0,3] 范围内 + if (isValidChunk(rest)) { + res.push(prev.concat(rest).join(".")) + } + return + } + + for (let i = 1; i <= 3; i++) { + let end = start + i + let cur = s.substring(start, end) + if (isValidChunk(cur)) { + findPos(end, prev.concat(cur), used + 1) + } + } + } + + findPos(0, [], 0) + + return res +} + +function isValidChunk(str) { + let strLen = str.length + if (strLen === 0) { + return false + } + // 开头是0的话 只能整个字符串只有一位0才行 + if (str[0] === "0") { + return strLen === 1 + } + let num = Number(str) + return num <= 255 +} From a016618e6b76ed5efc93a46eafbe56fc1d15bdcb Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 11 Jun 2020 11:28:41 +0800 Subject: [PATCH 092/145] =?UTF-8?q?feat:=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\205\261\347\245\226\345\205\210.js" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.js" diff --git "a/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.js" "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.js" new file mode 100644 index 0000000..5067d3c --- /dev/null +++ "b/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210.js" @@ -0,0 +1,57 @@ +let T = require("../工具/二叉树") + +let lowestCommonAncestor = function (root, p, q) { + let findAndCreate = (node, target, depth) => { + if (node !== target) { + let findInLeft + if (node.left) { + node.left.parent = node + findInLeft = findAndCreate(node.left, target, depth + 1) + } + + if (findInLeft) { + return findInLeft + } else { + if (node.right) { + node.right.parent = node + return findAndCreate(node.right, target, depth + 1) + } + } + } else { + return { + depth, + node, + } + } + } + + let findP = findAndCreate(root, p, 0) || {} + let findQ = findAndCreate(root, q, 0) || {} + + let cur = findP.depth > findQ.depth ? findQ.node : findP.node + + while (!(isAncestor(cur, p) && isAncestor(cur, q))) { + cur = cur.parent + } + + return cur +} + +function isAncestor(node, target) { + if (!node) { + return false + } + if (node !== target) { + return !!(isAncestor(node.left, target) || isAncestor(node.right, target)) + } else { + return true + } +} + +let l +let r +let t = new T(3) +t.left = l = new T(5) +t.right = r = new T(1) + +console.log(lowestCommonAncestor(t, l, r)) From aa04becfc3ea657261e80fc8f4cfd35c733bc74f Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 11 Jun 2020 11:36:19 +0800 Subject: [PATCH 093/145] docs: fix comment --- .../\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" "b/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" index 88945e5..c1ffaa6 100644 --- "a/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" +++ "b/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" @@ -6,9 +6,9 @@ let restoreIpAddresses = function (s) { let res = [] let findPos = (start, prev, used) => { if (used === 3) { - // 点全部用光后 剩余字符的长度不能超过3 就是一个答案 let rest = s.substr(start) - // 最后一位不能为0 且长度在 (0,3] 范围内 + // 点全部用光后 剩余字符依然是一个合格的ip chunk + // 就视为一个答案 放入数组 if (isValidChunk(rest)) { res.push(prev.concat(rest).join(".")) } From f08b1361825eee38f270394bdf768b3f8b5633bb Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 11 Jun 2020 16:27:01 +0800 Subject: [PATCH 094/145] =?UTF-8?q?feat:=20=E5=85=A8=E6=8E=92=E5=88=97?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\205\250\346\216\222\345\210\227-46.js" | 24 +++++++++ ...45\233\236\346\226\207\344\270\262-131.js" | 51 +++++++++++++++++++ ...5\216\237ip\345\234\260\345\235\200-93.js" | 0 ...346\257\215\347\273\204\345\220\210-17.js" | 0 4 files changed, 75 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227-46.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262-131.js" rename "DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" => "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" (100%) rename "DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" => "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" (100%) diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227-46.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227-46.js" new file mode 100644 index 0000000..59bc751 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227-46.js" @@ -0,0 +1,24 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +let permute = function (nums) { + let n = nums.length + if (n === 1) { + return [nums] + } + + let res = [] + for (let i = 0; i < n; i++) { + let use = nums[i] + let rest = nums.slice(0, i).concat(nums.slice(i + 1, n)) + let restPermuteds = permute(rest) + for (let restPermuted of restPermuteds) { + res.push(restPermuted.concat(use)) + } + } + + return res +} + +console.log(permute([1, 2, 3])) diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262-131.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262-131.js" new file mode 100644 index 0000000..c973e26 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\210\206\345\211\262\345\233\236\346\226\207\344\270\262-131.js" @@ -0,0 +1,51 @@ +/** + * @param {string} s + * @return {string[][]} + */ + +let partition = function (s) { + let n = s.length + let ret = [] + let find = function (start, prev) { + // 最少分割一个字符 最多分割到末尾前一位 + for (let i = 1; i <= n; i++) { + let end = start + i + let cur = s.substring(start, end) + if (cur) { + let res = prev.concat(cur) + if (isPalindrome(cur)) { + if (end === n) { + ret.push(res) + } else { + find(start + i, res) + } + } + } + } + } + find(0, []) + return ret +} + +function isPalindrome(s) { + if (!s) { + return false + } + let i = 0 + let j = s.length - 1 + + while (i < j) { + let head = s[i] + let tail = s[j] + + if (head !== tail) { + return false + } else { + i++ + j-- + } + } + return true +} + +console.log(partition("aab")) diff --git "a/DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" similarity index 100% rename from "DFS\351\227\256\351\242\230/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" rename to "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\244\215\345\216\237ip\345\234\260\345\235\200-93.js" diff --git "a/DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" similarity index 100% rename from "DFS\351\227\256\351\242\230/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" rename to "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\224\265\350\257\235\345\217\267\347\240\201\347\232\204\345\255\227\346\257\215\347\273\204\345\220\210-17.js" From e99d8bb98ef8951036e2dcc01a9934524c095735 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Thu, 11 Jun 2020 18:39:58 +0800 Subject: [PATCH 095/145] =?UTF-8?q?feat:=20=E5=85=A8=E6=8E=92=E5=88=97=20I?= =?UTF-8?q?I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\205\250\346\216\222\345\210\227 II-47.js" | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227 II-47.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227 II-47.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227 II-47.js" new file mode 100644 index 0000000..96278c2 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\205\250\346\216\222\345\210\227 II-47.js" @@ -0,0 +1,39 @@ + +let uniqSymbol = 'X' + +let permuteUnique = function (nums) { + let n = nums.length + if (n === 1) { + return [nums] + } + let permuteSet = (nums) => { + let n = nums.length + if (n === 0) { + return new Set() + } + if (n === 1) { + return new Set(nums) + } + + let res = new Set() + for (let i = 0; i < n; i++) { + let use = nums[i] + if (use === undefined) { + continue + } + let rest = nums.slice(0, i).concat(nums.slice(i + 1, n)) + let restPermuteds = permuteSet(rest) + restPermuteds.forEach((restPermuted) => { + res.add(`${use}${uniqSymbol}${restPermuted}`) + }) + } + + return res + } + + let permuted = permuteSet(nums) + + return Array.from(permuted).map((val) => val.split(uniqSymbol).map(Number)) +} + +console.log(permuteUnique([-1,2,-1,2,1,-1,2,1])) From b2f7317cb16e80c21ccb9a97578183c7d326a4dd Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 12 Jun 2020 01:17:10 +0800 Subject: [PATCH 096/145] =?UTF-8?q?feat:=20=E5=AD=90=E9=9B=86=E5=92=8C?= =?UTF-8?q?=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\255\220\351\233\206-78.js" | 32 +++++++++++++++++++ .../\347\273\204\345\220\210-77.js" | 28 ++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206-78.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206-78.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206-78.js" new file mode 100644 index 0000000..ca951d4 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206-78.js" @@ -0,0 +1,32 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +let subsets = function (nums) { + let res = [] + let n = nums.length + if (n === 0) { + return res + } + + let helper = (start, prev, targetLength) => { + if (start > n) { + return + } + if (prev.length === targetLength) { + res.push(prev) + return + } + + for (let i = start; i < n; i++) { + let cur = nums[i] + helper(i + 1, prev.concat(cur), targetLength) + } + } + + for (let j = 1; j <= nums.length; j++) { + helper(0, [], j) + } + + return [[], ...res] +} diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" new file mode 100644 index 0000000..7a1945c --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" @@ -0,0 +1,28 @@ +/** + * @param {number} n + * @param {number} k + * @return {number[][]} + */ +let combine = function (n, k) { + let ret = [] + + let helper = (start, prev) => { + let len = prev.length + if (len === k) { + ret.push(prev) + return + } + + if (start > n) { + return + } + + for (let i = start; i <= n; i++) { + helper(i + 1, prev.concat(i)) + } + } + helper(1, []) + return ret +} + +console.log(combine(4, 2)) From f2b87e1808bc37788422b625aeb3b6fdcdcbf7b9 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 12 Jun 2020 18:13:07 +0800 Subject: [PATCH 097/145] =?UTF-8?q?feat:=20=E7=BB=84=E5=90=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\273\204\345\220\210-77.js" | 5 ++ ...345\220\210\346\200\273\345\222\214-39.js" | 32 +++++++++++++ ...345\220\210\346\200\273\345\222\214-40.js" | 47 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-39.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-40.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" index 7a1945c..8f3ccf1 100644 --- "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210-77.js" @@ -17,7 +17,12 @@ let combine = function (n, k) { return } + // 还有 rest 个位置待填补 + let rest = k - prev.length for (let i = start; i <= n; i++) { + if (n - i + 1 < rest) { + continue + } helper(i + 1, prev.concat(i)) } } diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-39.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-39.js" new file mode 100644 index 0000000..f55dc03 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-39.js" @@ -0,0 +1,32 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +let combinationSum = function (candidates, target) { + let res = [] + + let helper = (start, prevSum, prevArr) => { + // 由于全是正整数 所以一旦和大于目标值了 直接结束本次递归即可。 + if (prevSum > target) { + return + } + // 目标值达成 + if (prevSum === target) { + res.push(prevArr) + return + } + + for (let i = start; i < candidates.length; i++) { + // 这里还是继续从start本身开始 因为多个重复值是允许的 + let cur = candidates[i] + let sum = prevSum + cur + let arr = prevArr.concat(cur) + helper(i, sum, arr) + } + } + + helper(0, 0, []) + + return res +} \ No newline at end of file diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-40.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-40.js" new file mode 100644 index 0000000..84a3750 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\273\204\345\220\210\346\200\273\345\222\214-40.js" @@ -0,0 +1,47 @@ +let genKey = (arr) => arr.join("~") +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +let combinationSum2 = function (candidates, target) { + let res = [] + + if (!candidates.length) { + return res + } + + candidates.sort() + + let used = {} + + let helper = (start, prevSum, prevArr) => { + // 由于全是正整数 所以一旦和大于目标值了 直接结束本次递归即可。 + if (prevSum > target) { + return + } + // 目标值达成 + if (prevSum === target) { + let key = genKey(prevArr) + if (!used[key]) { + res.push(prevArr) + used[key] = true + } + return + } + + for (let i = start; i < candidates.length; i++) { + // 这里还是继续从start本身开始 因为多个重复值是允许的 + let cur = candidates[i] + let sum = prevSum + cur + let arr = prevArr.concat(cur) + helper(i + 1, sum, arr) + } + } + + helper(0, 0, []) + + return res +} + +console.log(combinationSum2([2, 1, 2, 1, 3], 6)) From d1bad23a539fd059f29412e388a8e488ddac26a5 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 13 Jun 2020 16:45:48 +0800 Subject: [PATCH 098/145] =?UTF-8?q?feat:=20=E6=8E=92=E5=88=97=E7=BB=84?= =?UTF-8?q?=E5=90=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\210\266\346\211\213\350\241\250-401.js" | 54 +++++++++++++++++++ .../\345\255\220\351\233\206 II-90.js" | 45 ++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206 II-90.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" new file mode 100644 index 0000000..0a9ce85 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" @@ -0,0 +1,54 @@ +/** + * @param {number} num + * @return {string[]} + */ +let HOURS = [1, 2, 4, 8] +let MINUTES = [1, 2, 4, 8, 16, 32] + +let readBinaryWatch = function (num) { + let res = [] + + let combine = (arr, num) => { + if (num === 0) { + return [0] + } + let res = [] + let helper = (start, prevCount, prevSum) => { + if (prevCount === num) { + res.push(prevSum) + return + } + + for (let i = start; i < arr.length; i++) { + let cur = arr[i] + helper(i + 1, prevCount + 1, prevSum + cur) + } + } + helper(0, 0, 0) + return res + } + + for (let i = 0; i <= num; i++) { + let hours = combine(HOURS, i) + let minutes = combine(MINUTES, num - i) + + for (let hour of hours) { + if (hour > 11) continue + for (let minute of minutes) { + if (minute > 59) { + continue + } + res.push(`${hour}:${padLeft(minute)}`) + } + } + } + return res +} + +function padLeft(num) { + let str = num.toString() + if (str.length === 1) { + str = `0${str}` + } + return str +} \ No newline at end of file diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206 II-90.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206 II-90.js" new file mode 100644 index 0000000..d5eb09e --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\220\351\233\206 II-90.js" @@ -0,0 +1,45 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + let n = nums.length + let res = [] + if (!n) { + return res + } + + nums.sort() + + let used = {} + + let helper = (start, prev, target) => { + if (prev.length === target) { + let key = genKey(prev) + if (!used[key]) { + res.push(prev) + used[key] = true + } + return + } + + for (let i = start; i < n; i++) { + let rest = n - i + let need = target - prev.length + if (rest < need) { + continue + } + helper(i + 1, prev.concat(nums[i]), target) + } + } + + for (let i = 1; i <= n; i++) { + helper(0, [], i) + } + + return [[], ...res] +}; + +function genKey(arr) { + return arr.join('~') +} \ No newline at end of file From 4f5fbd4118410001d976a296b894b4bd571ff55f Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 14 Jun 2020 18:23:38 +0800 Subject: [PATCH 099/145] =?UTF-8?q?feat:=20=E5=8D=95=E8=AF=8D=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\257\215\346\220\234\347\264\242-79.js" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" new file mode 100644 index 0000000..c9306df --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" @@ -0,0 +1,64 @@ +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ +let directions = [[-1, 0], [1, 0], [0, -1], [0, 1]] // 左 右 上 下 + +let exist = function (board, word) { + let maxY = board.length + if (!maxY) return false + let maxX = board[0].length + + // 二维数组记录已访问过的元素 + let visited = new Array(maxY) + for (let y = 0; y < visited.length; y++) { + visited[y] = new Array(maxX) + } + + let inArea = (x, y) => { + return x >= 0 && x < maxX && y >= 0 && y < maxY + } + + let search = (startX, startY, wordIndex) => { + // 当前起始字符不匹配 直接失败 + let curCell = board[startY][startX] + let curChar = word[wordIndex] + if (curCell !== curChar) { + return false + } + + // 如果递归到最后一位字符 就直接返回最后一位字符是否匹配成功 + if (wordIndex === word.length - 1) { + return curChar === curChar + } + + // 进一步递归 先记录为已访问元素 防止递归的时候重复访问 + visited[startY][startX] = true + + for (let direction of directions) { + let [x, y] = direction + let nextX = startX + x + let nextY = startY + y + + // 需要保证未越界且未被访问过 + if (inArea(nextX, nextY) && !visited[nextY][nextX]) { + if (search(nextX, nextY, wordIndex + 1)) { + return true + } + } + } + // 重置已访问标记位 + visited[startY][startX] = false + } + + for (let y = 0; y < maxY; y++) { + for (let x = 0; x < maxX; x++) { + if (search(x, y, 0)) { + return true + } + } + } + + return false +}; \ No newline at end of file From c03198bc9c89c6f36fefe3abefd4ec53d68505c1 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Mon, 15 Jun 2020 17:35:38 +0800 Subject: [PATCH 100/145] =?UTF-8?q?feat:=20N=E7=9A=87=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../N \347\232\207\345\220\216-51.js" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/N \347\232\207\345\220\216-51.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/N \347\232\207\345\220\216-51.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/N \347\232\207\345\220\216-51.js" new file mode 100644 index 0000000..f3d623b --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/N \347\232\207\345\220\216-51.js" @@ -0,0 +1,70 @@ +/** + * @param {number} n + * @return {string[][]} + */ +let solveNQueens = function (n) { + let res = [] + + // 已摆放皇后的的列下标 + let columns = [] + // 已摆放皇后的对角线1下标 左下 -> 右上 + // 计算某个坐标是否在这个对角线的方式是「行下标 + 列下标」是否相等 + let dia1 = [] + // 已摆放皇后的对角线2下标 左上 -> 右下 + // 计算某个坐标是否在这个对角线的方式是「行下标 - 列下标」是否相等 + let dia2 = [] + + // 尝试在一个n皇后问题中 摆放第index行内的皇后位置 + let putQueen = (rowIndex, row) => { + if (rowIndex === n) { + res.push(generateBoard(row)) + return + } + + // 尝试摆第index行的皇后 尝试[0, n-1]列 + for (let columnIndex = 0; columnIndex < n; columnIndex++) { + // 在列上不冲突 + let columnNotConflict = !columns[columnIndex] + // 在对角线1上不冲突 + let dia1NotConflict = !dia1[rowIndex + columnIndex] + // 在对角线2上不冲突 + let dia2NotConflict = !dia2[rowIndex - columnIndex] + + if (columnNotConflict && dia1NotConflict && dia2NotConflict) { + + columns[columnIndex] = true + dia1[rowIndex + columnIndex] = true + dia2[rowIndex - columnIndex] = true + + putQueen(rowIndex + 1, row.concat(columnIndex)) + + columns[columnIndex] = false + dia1[rowIndex + columnIndex] = false + dia2[rowIndex - columnIndex] = false + } + } + } + + putQueen(0, []) + + return res +} + +function generateBoard(row) { + let n = row.length + let res = [] + for(let y = 0; y < n; y++) { + let cur = '' + for (let x = 0; x < n; x++) { + if (x === row[y]) { + cur += 'Q' + }else { + cur += '.' + } + } + res.push(cur) + } + return res +} + +console.log(solveNQueens(4)) From d1febbf130c3f624bb6049904b74499aa87a98cf Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Tue, 16 Jun 2020 00:54:34 +0800 Subject: [PATCH 101/145] =?UTF-8?q?feat:=20=E8=A7=A3=E6=95=B0=E7=8B=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\247\243\346\225\260\347\213\254-37.js" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\247\243\346\225\260\347\213\254-37.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\247\243\346\225\260\347\213\254-37.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\247\243\346\225\260\347\213\254-37.js" new file mode 100644 index 0000000..9bcf69e --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\247\243\346\225\260\347\213\254-37.js" @@ -0,0 +1,87 @@ +/** + * @param {character[][]} board + * @return {void} Do not return anything, modify board in-place instead. + */ +let solveSudoku = function (board) { + let rows = initTwoDimensionalArray(9) + let columns = initTwoDimensionalArray(9) + let grids = initTwoDimensionalArray(3) + + // 待处理下标队列 第一次扫描的时候记录下来 + let pending = [] + + for (let y = 0; y < 9; y++) { + for (let x = 0; x < 9; x++) { + let cell = board[y][x] + if (cell === ".") { + // 待填充的数独格子 记录在队列中 + pending.push([x, y]) + continue + } + // 记录下当前下标 + recordCell(x, y, cell) + } + } + + let helper = (startPendingIndex) => { + if (startPendingIndex === pending.length) { + return true + } + + let [x, y] = pending[startPendingIndex] + + for (let i = 1; i <= 9; i++) { + let cur = i.toString() + if (isValid(x, y, cur)) { + board[y][x] = cur + recordCell(x, y, cur) + if (helper(startPendingIndex + 1)) { + return true + } else { + board[y][x] = "." + restoreCell(x, y, cur) + } + } + } + } + + helper(0) + + function recordCell(x, y, cell) { + rows[y][cell] = true + columns[x][cell] = true + let [gridX, gridY] = findGridIndex(x, y) + if (!grids[gridY][gridX]) { + grids[gridY][gridX] = new Map() + } + grids[gridY][gridX].set(cell, true) + } + + function restoreCell(x, y, cell) { + rows[y][cell] = false + columns[x][cell] = false + let [gridX, gridY] = findGridIndex(x, y) + grids[gridY][gridX].set(cell, false) + } + + function isValid(x, y, cell) { + let isYConflict = rows[y][cell] + let isXConflict = columns[x][cell] + let [gridX, gridY] = findGridIndex(x, y) + let grid = grids[gridY][gridX] + let isGridConflict = grid && grid.get(cell) + return !isYConflict && !isXConflict && !isGridConflict + } +} + +function initTwoDimensionalArray(length) { + let ret = [] + for (let i = 0; i < length; i++) { + ret.push([]) + } + return ret +} + +function findGridIndex(x, y) { + return [Math.floor(x / 3), Math.floor(y / 3)] +} From c75acba8ff3156c9bf72569882f02f7ab77a4434 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 17 Jun 2020 10:55:27 +0800 Subject: [PATCH 102/145] =?UTF-8?q?feat:=20=E6=9C=80=E9=95=BF=E4=B8=8A?= =?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\267\257\345\276\204\345\222\214-120.js" | 28 +++++++++++++++++++ ...45\255\220\345\272\217\345\210\227-300.js" | 28 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\211\350\247\222\345\275\242\347\232\204\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-120.js" create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227-300.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\211\350\247\222\345\275\242\347\232\204\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-120.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\211\350\247\222\345\275\242\347\232\204\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-120.js" new file mode 100644 index 0000000..b802d89 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\211\350\247\222\345\275\242\347\232\204\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214-120.js" @@ -0,0 +1,28 @@ +/** + * @param {number[][]} triangle + * @return {number} + */ +let minimumTotal = function (triangle) { + let tryMin = (level, lastIndex, prevSum) => { + if (level === triangle.length) { + return prevSum + } + + let row = triangle[level] + let cur = row[lastIndex] + let curNext = row[lastIndex + 1] + + let selected = tryMin(level + 1, lastIndex, prevSum + cur) + if (curNext !== undefined) { + selected = Math.min( + selected, + tryMin(level + 1, lastIndex + 1, prevSum + curNext) + ) + } + return selected + } + + return tryMin(0, 0, 0) +} + +minimumTotal([[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227-300.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227-300.js" new file mode 100644 index 0000000..622c453 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\344\270\212\345\215\207\345\255\220\345\272\217\345\210\227-300.js" @@ -0,0 +1,28 @@ +/** + * @param {number[]} nums + * @return {number} + */ +let lengthOfLIS = function (nums) { + let dp = [] + let n = nums.length + if (!n) { + return 0 + } + + dp[0] = 1 + for (let i = 1; i < n; i++) { + let num = nums[i] + let max = 1 + // j 从 [0, i) 依次求出可以和 i 组成的最长上升子序列 + for (let j = 0; j < i; j++) { + let prevNum = nums[j] + if (num > prevNum) { + // 循环中不断更新 max 值 + max = Math.max(max, dp[j] + 1) + } + } + dp[i] = max + } + + return Math.max(...dp) +} From af7081ac0e432bba79b5f9a67f2f13d9a5420262 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 19 Jun 2020 13:18:15 +0800 Subject: [PATCH 103/145] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=B8=A4?= =?UTF-8?q?=E9=A2=98dp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\270\200\345\222\214\351\233\266-474.js" | 49 +++++++++++++++++++ ...47\233\256\346\240\207\345\222\214-494.js" | 39 +++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\347\233\256\346\240\207\345\222\214-494.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" new file mode 100644 index 0000000..69f703a --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" @@ -0,0 +1,49 @@ +/** + * @param {string[]} strs + * @param {number} m + * @param {number} n + * @return {number} + */ +let findMaxForm = function (strs, m, n) { + let sl = strs.length + if (!sl) { + return 0 + } + + let dp = [] + + for (let i = 0; i <= m; i++) { + dp[i] = [] + for (let j = 0; j <= n; j++) { + dp[i][j] = [] + for (let s = 0; s < sl; s++) { + let str = strs[s] + let [strM, strN] = countMAndN(str) + + let pickOnlyPrev = dp[i][j][s - 1] || 0 + let pickCurAndPrev = 0 + if (i >= strM && j >= strN) { + pickCurAndPrev = 1 + (dp[i - strM][j - strN][s - 1] || 0) + } + + dp[i][j][s] = Math.max(pickCurAndPrev, pickOnlyPrev) + } + } + } + return dp[m][n][sl - 1] +} + +function countMAndN(str) { + let m = 0 + let n = 0 + for (let i = 0; i < str.length; i++) { + if (str[i] === "0") { + m++ + } else { + n++ + } + } + return [m, n] +} + +console.log(findMaxForm(["10", "0", "1"], 1, 1)) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\347\233\256\346\240\207\345\222\214-494.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\347\233\256\346\240\207\345\222\214-494.js" new file mode 100644 index 0000000..d0b0687 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\347\233\256\346\240\207\345\222\214-494.js" @@ -0,0 +1,39 @@ +/** + * @param {number[]} nums + * @param {number} S + * @return {number} + */ +let findTargetSumWays = function (nums, S) { + let ns = nums.length + if (!ns) { + return 0 + } + let min = nums.reduce((sum, cur) => sum - cur, 0) + let max = nums.reduce((sum, cur) => sum + cur, 0) + + let dp = [] + for (let n = 0; n < ns; n++) { + dp[n] = [] + } + + // 基础状态 + for (let s = min; s <= max; s++) { + let num = nums[0] + let pickPositive = s === num ? 1 : 0 + // 选负数形态 + let pickNegative = -s === num ? 1 : 0 + dp[0][s] = pickPositive + pickNegative + } + + for (let n = 1; n < ns; n++) { + for (let s = min; s <= max; s++) { + let num = nums[n] + // 选正数形态 + let pickPositive = dp[n - 1][s - num] || 0 + // 选负数形态 + let pickNegative = dp[n - 1][s + num] || 0 + dp[n][s] = pickNegative + pickPositive + } + } + return dp[ns - 1][S] || 0 +} From 1cc56fa3686bb86b10e955e0bda54a6a9d348862 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 20 Jun 2020 22:55:03 +0800 Subject: [PATCH 104/145] =?UTF-8?q?feat:=20=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\217\221\351\245\274\345\271\262-455.js" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" diff --git "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" new file mode 100644 index 0000000..4fb5d6a --- /dev/null +++ "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" @@ -0,0 +1,28 @@ +/** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ +var findContentChildren = function (g, s) { + g.sort((a, b) => a - b) + s.sort((a, b) => a - b) + + let i = 0 + let j = 0 + + let count = 0 + while (j < s.length && i < g.length) { + let need = g[i] + let cookie = s[j] + + if (cookie >= need) { + count++ + i++ + j++ + } else { + j++ + } + } + + return count +} From 552d8e1dc42da2f610b86c5caba46e8c3d9431de Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 21 Jun 2020 00:51:14 +0800 Subject: [PATCH 105/145] =?UTF-8?q?feat:=20=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\217\221\351\245\274\345\271\262-455.js" | 2 +- ...45\255\220\345\272\217\345\210\227-392.js" | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 "\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" diff --git "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" index 4fb5d6a..8af19cb 100644 --- "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" +++ "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\206\345\217\221\351\245\274\345\271\262-455.js" @@ -3,7 +3,7 @@ * @param {number[]} s * @return {number} */ -var findContentChildren = function (g, s) { +let findContentChildren = function (g, s) { g.sort((a, b) => a - b) s.sort((a, b) => a - b) diff --git "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" new file mode 100644 index 0000000..8448ad4 --- /dev/null +++ "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" @@ -0,0 +1,25 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +let isSubsequence = function (s, t) { + let i = 0 + let sl = s.length + if (!sl) { + return true + } + + for (let j = 0; j < t.length; j++) { + let target = s[i] + let cur = t[j] + if (cur === target) { + i++ + if (i === sl) { + return true + } + } + } + + return false +} From c9ddba8a422ad45953a6195c6926c5a46ec36cdc Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 21 Jun 2020 01:18:36 +0800 Subject: [PATCH 106/145] =?UTF-8?q?refactor(=E5=88=A4=E6=96=AD=E5=AD=90?= =?UTF-8?q?=E5=BA=8F=E5=88=97):=20change=20i=20position?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\255\220\345\272\217\345\210\227-392.js" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" index 8448ad4..bf9f8f1 100644 --- "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" +++ "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" @@ -23,3 +23,28 @@ let isSubsequence = function (s, t) { return false } +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +let isSubsequence = function (s, t) { + let sl = s.length + if (!sl) { + return true + } + + let i = 0 + for (let j = 0; j < t.length; j++) { + let target = s[i] + let cur = t[j] + if (cur === target) { + i++ + if (i === sl) { + return true + } + } + } + + return false +} From 0d257c5e0e765bb990b9b64fef2600d19ee068f5 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 21 Jun 2020 20:43:54 +0800 Subject: [PATCH 107/145] =?UTF-8?q?feat:=20=E6=9C=80=E9=95=BF=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E5=AD=90=E5=BA=8F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\255\220\345\272\217\345\210\227-1143.js" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227-1143.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227-1143.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227-1143.js" new file mode 100644 index 0000000..71c8e51 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227-1143.js" @@ -0,0 +1,32 @@ +/** + * @param {string} text1 + * @param {string} text2 + * @return {number} + */ +let longestCommonSubsequence = function (text1, text2) { + let n1 = text1.length + let n2 = text2.length + + let dp = [] + + for (let i1 = 0; i1 <= n1; i1++) { + dp[i1] = [] + dp[i1][0] = 0 + } + dp[0] = Array(n2 + 1).fill(0) + + for (let i1 = 1; i1 <= n1; i1++) { + for (let i2 = 1; i2 <= n2; i2++) { + let str1 = text1[i1 - 1] + let str2 = text2[i2 - 1] + + if (str1 === str2) { + dp[i1][i2] = 1 + dp[i1 - 1][i2 - 1] + }else { + dp[i1][i2] = Math.max(dp[i1 - 1][i2], dp[i1][i2 - 1]) + } + } + } + + return dp[n1][n2] +}; \ No newline at end of file From ba0e5b2f89f6ac0bf3c7955464468c78f9bd6060 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 21 Jun 2020 21:40:35 +0800 Subject: [PATCH 108/145] =?UTF-8?q?feat:=20=E6=97=A0=E9=87=8D=E5=8F=A0?= =?UTF-8?q?=E5=8C=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\217\240\345\214\272\351\227\264-435.js" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264-435.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264-435.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264-435.js" new file mode 100644 index 0000000..c95bbf1 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\227\240\351\207\215\345\217\240\345\214\272\351\227\264-435.js" @@ -0,0 +1,31 @@ +/** + * @param {number[][]} intervals + * @return {number} + */ +let eraseOverlapIntervals = function (intervals) { + let n = intervals.length + if (!n) { + return 0 + } + + // 按照起始点排序 + intervals.sort((a, b) => a[0] - b[0]) + + // dp[i] 表示从 [0, i] 能构成的最长的无重叠区间的个数 + let dp = [] + dp[0] = 1 + + for (let i = 1; i < n; i++) { + let max = 1 + let [curStart] = intervals[i] + for (let j = 0; j < i; j++) { + let [prevStart, prevEnd] = intervals[j] + if (prevEnd <= curStart) { + max = Math.max(max, dp[j] + 1) + } + } + dp[i] = max + } + + return n - Math.max(...dp) +}; \ No newline at end of file From 28a87164acd41c32e7ef0668cd58091be113c735 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Tue, 23 Jun 2020 22:24:04 +0800 Subject: [PATCH 109/145] =?UTF-8?q?feat:=20=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\226\207\345\255\220\344\270\262-5.js" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" new file mode 100644 index 0000000..a7e91d7 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" @@ -0,0 +1,42 @@ +/** + * @param {string} s + * @return {string} + */ +let longestPalindrome = function (s) { + let n = s.length + if (n < 2) { + return s + } + + let dp = [] + for (let i = 0; i < n; i++) { + dp[i] = [] + dp[i][i] = true + } + + let max = 0 + let begin = 0 + for (let j = 1; j < n; j++) { + for (let i = 0; i < j; i++) { + if (s[j] !== s[i]) { + dp[i][j] = false + } else { + let indent = dp[i + 1][j - 1] + if (indent === undefined || indent === true) { + dp[i][j] = true + }else { + dp[i][j] = false + } + } + + if (dp[i][j] === true && j - i > max) { + max = j - i + begin = i + } + } + } + console.log('dp', dp) + return s.substr(begin, max + 1) +} + +console.log(longestPalindrome("abcda")) From c5f611e8bb55cbbd78b316275de11fb96c4267ef Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Wed, 24 Jun 2020 05:42:10 +0800 Subject: [PATCH 110/145] =?UTF-8?q?feat:=20=E6=9C=80=E9=95=BF=E5=9B=9E?= =?UTF-8?q?=E6=96=87=E5=AD=90=E4=B8=B2-=E4=B8=AD=E5=BF=83=E6=89=A9?= =?UTF-8?q?=E6=95=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\226\207\345\255\220\344\270\262-5.js" | 85 +++++++++++++------ ...46\226\207\345\255\220\344\270\262-647.js" | 29 +++++++ 2 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 "\345\233\236\346\226\207\345\255\220\344\270\262-647.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" index a7e91d7..9876ba0 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262-5.js" @@ -1,4 +1,47 @@ /** + * 动态规划 + * @param {string} s + * @return {string} + */ +// let longestPalindrome = function (s) { +// let n = s.length +// if (n < 2) { +// return s +// } + +// let dp = [] +// for (let i = 0; i < n; i++) { +// dp[i] = [] +// dp[i][i] = true +// } + +// let max = 0 +// let begin = 0 +// for (let j = 1; j < n; j++) { +// for (let i = 0; i < j; i++) { +// if (s[j] !== s[i]) { +// dp[i][j] = false +// } else { +// let indent = dp[i + 1][j - 1] +// if (indent === undefined || indent === true) { +// dp[i][j] = true +// }else { +// dp[i][j] = false +// } +// } + +// if (dp[i][j] === true && j - i > max) { +// max = j - i +// begin = i +// } +// } +// } +// console.log('dp', dp) +// return s.substr(begin, max + 1) +// } + +/** + * 中心扩散法 * @param {string} s * @return {string} */ @@ -8,35 +51,27 @@ let longestPalindrome = function (s) { return s } - let dp = [] - for (let i = 0; i < n; i++) { - dp[i] = [] - dp[i][i] = true - } - - let max = 0 let begin = 0 - for (let j = 1; j < n; j++) { - for (let i = 0; i < j; i++) { - if (s[j] !== s[i]) { - dp[i][j] = false - } else { - let indent = dp[i + 1][j - 1] - if (indent === undefined || indent === true) { - dp[i][j] = true - }else { - dp[i][j] = false - } - } + let max = 1 - if (dp[i][j] === true && j - i > max) { - max = j - i - begin = i + let spread = (start, end) => { + while (s[start] === s[end] && start >= 0 && end < n) { + let len = end - start + 1 + if (len > max) { + max = len + begin = start } + start-- + end++ } } - console.log('dp', dp) - return s.substr(begin, max + 1) + + for (let mid = 0; mid < n; mid++) { + spread(mid, mid) + spread(mid, mid + 1) + } + + return s.substr(begin, max) } -console.log(longestPalindrome("abcda")) +console.log(longestPalindrome("babad")) diff --git "a/\345\233\236\346\226\207\345\255\220\344\270\262-647.js" "b/\345\233\236\346\226\207\345\255\220\344\270\262-647.js" new file mode 100644 index 0000000..3a9c237 --- /dev/null +++ "b/\345\233\236\346\226\207\345\255\220\344\270\262-647.js" @@ -0,0 +1,29 @@ +/** + * @param {string} s + * @return {number} + */ +let countSubstrings = function (s) { + let n = s.length + if (n < 2) { + return n + } + + let count = 0 + + let spread = (start, end) => { + while (s[start] === s[end] && start >= 0 && end < n) { + start-- + end++ + count++ + } + } + + for (let mid = 0; mid < n; mid++) { + spread(mid, mid) + spread(mid, mid + 1) + } + + return count +} + +console.log(countSubstrings("a")) From b7d1eb39dde84debfc53f0f137bf642f9b45b988 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 25 Jun 2020 12:31:52 +0800 Subject: [PATCH 111/145] =?UTF-8?q?feat:=20=E5=8D=95=E8=AF=8D=E6=8B=86?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...50\257\215\346\213\206\345\210\206-139.js" | 25 ++++ ...42 II-212.Trie\347\211\210\346\234\254.js" | 112 ++++++++++++++++++ ...257\215\346\220\234\347\264\242 II-212.js" | 87 ++++++++++++++ 3 files changed, 224 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.Trie\347\211\210\346\234\254.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" new file mode 100644 index 0000000..dbb4672 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" @@ -0,0 +1,25 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +let wordBreak = function (s, wordDict) { + let n = s.length + if (!n) return true + + let wordSet = new Set(wordDict) + let dp = [] + dp[0] = true + + for (let i = 0; i <= n; i++) { + for (let j = i; j >= 0; j--) { + let word = s.slice(j, i) + if (wordSet.has(word) && dp[j]) { + dp[i] = true + break + } + } + } + + return !!dp[n] +} diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.Trie\347\211\210\346\234\254.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.Trie\347\211\210\346\234\254.js" new file mode 100644 index 0000000..9032686 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.Trie\347\211\210\346\234\254.js" @@ -0,0 +1,112 @@ +/** + * Initialize your data structure here. + */ +var Trie = function () { + this.root = new TrieNode() +} + +var TrieNode = function () { + this.children = new Map() + this.isEnd = false +} + +/** + * Inserts a word into the trie. + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function (word) { + let node = this.root + + for (let i = 0; i < word.length; i++) { + let { children } = node + let trieNode = children.get(word[i]) + if (!trieNode) { + trieNode = new TrieNode() + children.set(word[i], trieNode) + } + node = trieNode + + if (i === word.length - 1) { + node.isEnd = true + node.word = word + } + } +} + +let dirs = [ + [0, 1], + [0, -1], + [-1, 0], + [1, 0], +] +/** + * @param {character[][]} board + * @param {string[]} words + * @return {string[]} + */ +let findWords = function (board, words) { + let maxY = board.length + if (!maxY) return [] + let maxX = board[0].length + + let rootTrie = new Trie() + for (let word of words) { + rootTrie.insert(word) + } + + // 记录已访问过的二维下标 + let visited = [] + for (let y = 0; y < maxY; y++) { + visited[y] = [] + } + + let isValid = (x, y) => { + return x >= 0 && x < maxX && y >= 0 && y < maxY && !visited[y][x] + } + + // 返回结果 + let res = [] + + let dfs = (x, y, trie) => { + let char = board[y][x] + let children = trie.children + let nextTrie = children && children.get(char) + if (nextTrie) { + if (nextTrie.word) { + res.push(nextTrie.word) + nextTrie.word = null + } else { + visited[y][x] = true + for (let dir of dirs) { + let [offsetY, offsetX] = dir + let nextY = y + offsetY + let nextX = x + offsetX + if (isValid(nextX, nextY)) { + dfs(nextX, nextY, nextTrie) + } + } + visited[y][x] = false + } + } + } + + for (let y = 0; y < maxY; y++) { + for (let x = 0; x < maxX; x++) { + if (y === 1 && x === 0) debugger + dfs(x, y, rootTrie.root) + } + } + + return Array.from(new Set(res)) +} + +console.log( + findWords( + [ + ["a", "b"], + ["a", "a"], + ], + ["aaba"] + ) +) diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.js" new file mode 100644 index 0000000..66d2f3d --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242 II-212.js" @@ -0,0 +1,87 @@ +let dirs = [ + [0, 1], + [0, -1], + [-1, 0], + [1, 0], +] +/** + * @param {character[][]} board + * @param {string[]} words + * @return {string[]} + */ +let findWords = function (board, words) { + let maxY = board.length + if (!maxY) return [] + let maxX = board[0].length + + // 记录已访问过的二维下标 + let visited = [] + for (let y = 0; y < maxY; y++) { + visited[y] = [] + } + + let isValid = (x, y) => { + return x >= 0 && x < maxX && y >= 0 && y < maxY && !visited[y][x] + } + + // 返回结果 + let res = [] + + let dfs = (x, y, word, index) => { + let char = board[y][x] + let targetChar = word[index] + if (char === targetChar) { + if (index === word.length - 1) { + res.push(word) + } else { + visited[y][x] = true + for (let dir of dirs) { + let [offsetY, offsetX] = dir + let nextY = y + offsetY + let nextX = x + offsetX + if (isValid(nextX, nextY)) { + dfs(nextX, nextY, word, index + 1) + } + } + visited[y][x] = false + } + } + } + + let prefixMap = new Map() + for (let y = 0; y < maxY; y++) { + for (let x = 0; x < maxX; x++) { + let prefix = board[y][x] + let pos = prefixMap.get(prefix) + if (!pos) { + prefixMap.set(prefix, [[y, x]]) + } else { + prefixMap.set(prefix, [...pos, [y, x]]) + } + } + } + + let find = (word) => { + let head = word[0] + let pos = prefixMap.get(head) + if (pos) { + pos.forEach(([y, x]) => { + dfs(x, y, word, 0) + }) + } + } + + words.forEach(find) + + return Array.from(new Set(res)) +} + +console.log( + findWords( + [ + ["a", "b"], + ["a", "a"], + ], + ["aba", "baa", "bab", "aaab", "aaa", "aaaa", "aaba"] + ) +) From ed4deb88c5f0729c31342cc0bcb99a2e3d15cad3 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 25 Jun 2020 13:34:54 +0800 Subject: [PATCH 112/145] =?UTF-8?q?feat:=20=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\225\260\347\233\270\345\212\240-3.js" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" diff --git "a/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" "b/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" new file mode 100644 index 0000000..9eec915 --- /dev/null +++ "b/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" @@ -0,0 +1,59 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +let addTwoNumbers = function (l1, l2) { + let i = 0 + let root = new ListNode() + let cur = root + let plus = false + + let traverse = (node1, node2) => { + let isDouble = !!node2 + while (isDouble ? node1 && node2 : node1) { + cur.next = new ListNode() + cur = cur.next + + let sum = node1.val + (plus ? 1 : 0) + if (isDouble) { + sum += node2.val + } + + if (sum >= 10) { + sum %= 10 + plus = true + } else { + plus = false + } + cur.val = sum + + node1 = node1.next + if (isDouble) { + node2 = node2.next + } + } + + if (node1) { + traverse(node1) + } + if (node2) { + traverse(node2) + } + } + + traverse(l1, l2) + + if (plus) { + cur.next = new ListNode(1) + } + + return root.next +} From bbceee072bbab560d262e279657a1adb87baf21b Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 25 Jun 2020 16:35:36 +0800 Subject: [PATCH 113/145] =?UTF-8?q?feat:=20=E4=B8=A4=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...225\260\347\233\270\345\212\240 II-445.js" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240 II-445.js" diff --git "a/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240 II-445.js" "b/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240 II-445.js" new file mode 100644 index 0000000..477c5a7 --- /dev/null +++ "b/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240 II-445.js" @@ -0,0 +1,69 @@ +var reverseList = function (head) { + if (!head) return null + let res = null + let dfs = function (node) { + if (node.next) { + dfs(node.next) + node.next.next = node + } else { + res = node + } + } + + dfs(head) + + head.next = null + + return res +}; + +var addTwoNumbers = function (l1, l2) { + l1 = reverseList(l1) + l2 = reverseList(l2) + + let i = 0 + let root = new ListNode() + let cur = root + let plus = false + + let traverse = (node1, node2) => { + let isDouble = !!node2 + while (isDouble ? (node1 && node2) : node1) { + cur.next = new ListNode() + cur = cur.next + + let sum = node1.val + (plus ? 1 : 0) + if (isDouble) { + sum += node2.val + } + + if (sum >= 10) { + sum %= 10 + plus = true + } else { + plus = false + } + cur.val = sum + + node1 = node1.next + if (isDouble) { + node2 = node2.next + } + } + + if (node1) { + traverse(node1) + } + if (node2) { + traverse(node2) + } + } + + traverse(l1, l2) + + if (plus) { + cur.next = new ListNode(1) + } + + return reverseList(root.next) +}; From 62e73148edd3e0c24a6cf259d02daea79c15e572 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 25 Jun 2020 23:11:58 +0800 Subject: [PATCH 114/145] =?UTF-8?q?feat:=20=E5=8D=95=E8=AF=8D=E6=8B=86?= =?UTF-8?q?=E5=88=86=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...350\257\215\346\213\206\345\210\206 II.js" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206 II.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206 II.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206 II.js" new file mode 100644 index 0000000..06c432a --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206 II.js" @@ -0,0 +1,37 @@ +let wordBreak = function (s, wordDict) { + let uniqSChars = uniq(s.split("")) + let uniqWordDictChars = uniq(wordDict.join("")) + if (uniqSChars.length !== uniqWordDictChars.length) { + return false + } + + let n = s.length + if (!n) { + return [] + } + + let wordSet = new Set(wordDict) + let dp = [] + dp[0] = [""] + + for (let i = 1; i <= n; i++) { + let res = [] + for (let j = i; j >= 0; j--) { + let word = s.slice(j, i) + if (wordSet.has(word)) { + if (dp[j] && dp[j].length) { + for (let prev of dp[j]) { + res.push(prev ? prev + " " + word : word) + } + } + } + } + dp[i] = res + } + + return dp[n] +} + +function uniq(arr) { + return Array.from(new Set(arr)) +} From 622e9dafa3dee5a7419b17e27e3cb650e263da5b Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 26 Jun 2020 02:22:01 +0800 Subject: [PATCH 115/145] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...273\264\347\237\251\351\230\265 II-240.js" | 24 +++++++++++++++++++ ...50\241\250\345\205\203\347\264\240-203.js" | 18 ++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 "\345\217\214\346\214\207\351\222\210/\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II-240.js" create mode 100644 "\351\223\276\350\241\250/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240-203.js" diff --git "a/\345\217\214\346\214\207\351\222\210/\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II-240.js" "b/\345\217\214\346\214\207\351\222\210/\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II-240.js" new file mode 100644 index 0000000..54f7a34 --- /dev/null +++ "b/\345\217\214\346\214\207\351\222\210/\346\220\234\347\264\242\344\272\214\347\273\264\347\237\251\351\230\265 II-240.js" @@ -0,0 +1,24 @@ +/** + * @param {number[][]} matrix + * @param {number} target + * @return {boolean} + */ +let searchMatrix = function (matrix, target) { + let y = matrix.length + if (!y) return false + let x = matrix[0].length + + let row = y - 1 + let column = 0 + while (row >= 0 && column < x) { + let val = matrix[row][column] + if (val > target) { + row-- + } else if (val < target) { + column++ + } else if (val === target) { + return true + } + } + return false +}; \ No newline at end of file diff --git "a/\351\223\276\350\241\250/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240-203.js" "b/\351\223\276\350\241\250/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240-203.js" new file mode 100644 index 0000000..bd16019 --- /dev/null +++ "b/\351\223\276\350\241\250/\347\247\273\351\231\244\351\223\276\350\241\250\345\205\203\347\264\240-203.js" @@ -0,0 +1,18 @@ +let removeElements = function (head, val) { + let root = new ListNode() + root.next = head + let cur = root + while (cur) { + let next = cur.next + if (!next) { + break + } + let nextVal = next.val + if (nextVal === val) { + cur.next = cur.next.next + } else { + cur = cur.next + } + } + return root.next +} From 660c789dc9c201b9404dd0a268504fa6f411c8dc Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 26 Jun 2020 14:37:18 +0800 Subject: [PATCH 116/145] Update README.md --- README.md | 403 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 403 insertions(+) diff --git a/README.md b/README.md index c2fd139..9b7fb9c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,409 @@ 思路会记录在本仓库的 Issues 中,按照 label 进行分类。比如想查看 「DFS」 分类下的问题,那么选择标签进行筛选即可。 +## 目录 +### 待复习 + +[移除链表元素-203](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/97) + +[搜索二维矩阵 II-240](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/96) + +[单词拆分-139](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/93) + +[最长回文子串-5](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/91) + +[无重叠区间-435](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/90) + +[目标和-494](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/87) + +[最长公共子序列-1143](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/85) + +[三角形最小路径和-120](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/80) + +[N皇后-51](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/78) + +[组合总和 II-40](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/73) + +[组合总和-39](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/72) + +[子集-78](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/71) + +[全排列-46](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/68) + +[复原IP地址-93](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/66) + +[电话号码的字母组合-17](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/65) + +[将有序数组转换为二叉搜索树](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/63) + +[删除二叉搜索树中的节点-450](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/62) + +[路径总和 III-437](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/61) + +[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) + +[对称二叉树-101](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/55) + +[两两交换链表中的节点-24](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/51) + +[二叉树的前序遍历-144](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/50) + +[找到字符串中所有字母异位词-438](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/44) + +[最小覆盖子串-76](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/43) + +[无重复字符的最长子串-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/42) + +[长度最小的子数组-209](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/36) + +[LRU 缓存机制-146](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/35) + +[括号生成-22](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/31) + +[合并两个有序数组-88](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/29) + +[移动零-283](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/26) + +[最大子序和-53](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/17) + +[背包(01背包)](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/15) + +### 链表 + +[移除链表元素-203](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/97) + +[两数相加-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/94) + +[两两交换链表中的节点-24](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/51) + +[删除链表的倒数第N个节点-19](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/46) + +[删除链表的节点-面试题18](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/40) + +[反转链表II-92](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/39) + +[反转链表 206](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/38) + +### 双指针 + +[搜索二维矩阵 II-240](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/96) + +[判断子序列-392](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/89) + +[分发饼干-455](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/88) + +[验证回文串-125](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/33) + +[两数之和 II - 输入有序数组-167](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/32) + +[合并两个有序数组-88](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/29) + +[移动零-283](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/26) + +[删除排序数组中的重复项-26](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/8) + +[盛水最多的容器-11](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/3) + +### 动态规划 + +[单词拆分 II-140](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/95) + +[单词拆分-139](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/93) + +[最长回文子串-5](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/91) + +[无重叠区间-435](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/90) + +[目标和-494](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/87) + +[一和零-474](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/86) + +[最长公共子序列-1143](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/85) + +[摆动序列-376](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/84) + +[最长上升子序列-300](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/83) + +[最长等差数列-1027](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/82) + +[解码方法-91](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/81) + +[三角形最小路径和-120](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/80) + +[最小路径和-64](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/34) + +[括号生成-22](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/31) + +[爬楼梯-70](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/22) + +[买卖股票的最佳时机-121](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/19) + +[乘积最大子数组-152](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/18) + +[最大子序和-53](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/17) + +[分割等和子集(01背包的变种)-416](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/16) + +[背包(01背包)](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/15) + +[使用最小花费爬楼梯-746](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/13) + +[零钱兑换 II-518](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/12) + +[打家劫舍 - 198](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/10) + +[完全平方数-279](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/9) + +[整数拆分-343](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/7) + +[斐波那契数-509](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/2) + +### 递归与回溯 + +[单词搜索 II-212](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/92) + +[解数独-37](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/79) + +[N皇后-51](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/78) + +[单词搜索-79](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/77) + +[二进制手表-401](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/76) + +[子集 II-90](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/75) + +[ 组合总和 III-216](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/74) + +[组合总和 II-40](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/73) + +[组合总和-39](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/72) + +[子集-78](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/71) + +[组合-77](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/70) + +[全排列 II-47](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/69) + +[全排列-46](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/68) + +[分割回文串-131](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/67) + +[复原IP地址-93](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/66) + +[电话号码的字母组合-17](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/65) + +### 贪心算法 + +[判断子序列-392](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/89) + +[分发饼干-455](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/88) + +[买卖股票的最佳时机 II-122](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/20) + +### 例题详解 + +[分发饼干-455](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/88) + +[N皇后-51](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/78) + +[单词搜索-79](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/77) + +[二进制手表-401](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/76) + +[电话号码的字母组合-17](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/65) + +[二叉树的所有路径-257](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/59) + +[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) + +[两两交换链表中的节点-24](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/51) + +[无重复字符的最长子串-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/42) + +[二分查找-704](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/23) + +[背包(01背包)](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/15) + +[盛水最多的容器-11](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/3) + +[斐波那契数-509](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/2) + +### DFS + +[二叉树的最近公共祖先-236](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/64) + +[将有序数组转换为二叉搜索树](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/63) + +[删除二叉搜索树中的节点-450](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/62) + +[路径总和 III-437](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/61) + +[求根到叶子节点数字之和-129](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/60) + +[二叉树的所有路径-257](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/59) + +[左叶子之和-404](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/58) + +[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) + +[平衡二叉树-110](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/56) + +[对称二叉树-101](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/55) + +[二叉树的最小深度-111](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/54) + +[二叉树的最大深度-104](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/53) + +[二叉树的层序遍历](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/30) + +[路径总和 II-113](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/27) + +[相同的树-100](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/21) + +[打家劫舍 |||-337](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/11) + +[被围绕的区域-130](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/6) + +[岛屿的最大面积-695](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/5) + +### 二叉树 + +[二叉树的最近公共祖先-236](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/64) + +[将有序数组转换为二叉搜索树](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/63) + +[删除二叉搜索树中的节点-450](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/62) + +[路径总和 III-437](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/61) + +[求根到叶子节点数字之和-129](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/60) + +[二叉树的所有路径-257](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/59) + +[左叶子之和-404](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/58) + +[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) + +[平衡二叉树-110](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/56) + +[对称二叉树-101](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/55) + +[二叉树的最小深度-111](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/54) + +[二叉树的最大深度-104](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/53) + +[二叉树的右视图-199](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/52) + +[二叉树的前序遍历-144](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/50) + +[二叉树的层序遍历](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/30) + +[路径总和 II-113](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/27) + +[相同的树-100](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/21) + +### BFS + +[二叉树的最小深度-111](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/54) + +[二叉树的最大深度-104](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/53) + +[二叉树的右视图-199](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/52) + +[二叉树的层序遍历](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/30) + +[相同的树-100](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/21) + +[在每个树行中找最大值-515](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/4) + +### 栈和队列 + +[二叉树的右视图-199](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/52) + +[二叉树的前序遍历-144](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/50) + +[简化路径-71](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/49) + +[有效的括号-20](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/48) + +[逆波兰表达式求值-150](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/47) + +### 滑动窗口 + +[滑动窗口的最大值-239](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/45) + +[找到字符串中所有字母异位词-438](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/44) + +[最小覆盖子串-76](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/43) + +[无重复字符的最长子串-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/42) + +[长度最小的子数组-209](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/36) + +### 排序 + +[快速排序](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/41) + +[颜色分类-75](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/28) + +### 查找表 + +[两个数组的交集 II-350](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/37) + +### 数据结构 + +[LRU 缓存机制-146](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/35) + +[实现 Trie (前缀树)-208](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/14) + +### 复习 * 1 + +[两数之和 II - 输入有序数组-167](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/32) + +[Pow(x, n)-50](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/25) + +[x 的平方根-69](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/24) + +[爬楼梯-70](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/22) + +[买卖股票的最佳时机 II-122](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/20) + +[买卖股票的最佳时机-121](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/19) + +[分割等和子集(01背包的变种)-416](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/16) + +[实现 Trie (前缀树)-208](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/14) + +[零钱兑换 II-518](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/12) + +[打家劫舍 |||-337](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/11) + +[删除排序数组中的重复项-26](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/8) + +[被围绕的区域-130](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/6) + +### 二分查找 + +[Pow(x, n)-50](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/25) + +[x 的平方根-69](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/24) + +[二分查找-704](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/23) + +### 复习 * 3 + +[乘积最大子数组-152](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/18) + +[最大子序和-53](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/17) + +### 复习 * 2 + +[打家劫舍 - 198](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/10) + +[整数拆分-343](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/7) + +[在每个树行中找最大值-515](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/4) + ## Author 👤 **ssh** From f36eb3081791984f1c9424f7e96ae151780c757a Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 26 Jun 2020 14:40:34 +0800 Subject: [PATCH 117/145] Update README.md --- README.md | 97 ------------------------------------------------------- 1 file changed, 97 deletions(-) diff --git a/README.md b/README.md index 9b7fb9c..0e2a61d 100644 --- a/README.md +++ b/README.md @@ -16,71 +16,6 @@ 思路会记录在本仓库的 Issues 中,按照 label 进行分类。比如想查看 「DFS」 分类下的问题,那么选择标签进行筛选即可。 ## 目录 -### 待复习 - -[移除链表元素-203](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/97) - -[搜索二维矩阵 II-240](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/96) - -[单词拆分-139](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/93) - -[最长回文子串-5](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/91) - -[无重叠区间-435](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/90) - -[目标和-494](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/87) - -[最长公共子序列-1143](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/85) - -[三角形最小路径和-120](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/80) - -[N皇后-51](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/78) - -[组合总和 II-40](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/73) - -[组合总和-39](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/72) - -[子集-78](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/71) - -[全排列-46](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/68) - -[复原IP地址-93](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/66) - -[电话号码的字母组合-17](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/65) - -[将有序数组转换为二叉搜索树](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/63) - -[删除二叉搜索树中的节点-450](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/62) - -[路径总和 III-437](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/61) - -[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) - -[对称二叉树-101](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/55) - -[两两交换链表中的节点-24](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/51) - -[二叉树的前序遍历-144](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/50) - -[找到字符串中所有字母异位词-438](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/44) - -[最小覆盖子串-76](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/43) - -[无重复字符的最长子串-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/42) - -[长度最小的子数组-209](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/36) - -[LRU 缓存机制-146](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/35) - -[括号生成-22](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/31) - -[合并两个有序数组-88](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/29) - -[移动零-283](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/26) - -[最大子序和-53](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/17) - -[背包(01背包)](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/15) ### 链表 @@ -370,32 +305,6 @@ [实现 Trie (前缀树)-208](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/14) -### 复习 * 1 - -[两数之和 II - 输入有序数组-167](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/32) - -[Pow(x, n)-50](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/25) - -[x 的平方根-69](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/24) - -[爬楼梯-70](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/22) - -[买卖股票的最佳时机 II-122](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/20) - -[买卖股票的最佳时机-121](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/19) - -[分割等和子集(01背包的变种)-416](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/16) - -[实现 Trie (前缀树)-208](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/14) - -[零钱兑换 II-518](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/12) - -[打家劫舍 |||-337](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/11) - -[删除排序数组中的重复项-26](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/8) - -[被围绕的区域-130](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/6) - ### 二分查找 [Pow(x, n)-50](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/25) @@ -404,12 +313,6 @@ [二分查找-704](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/23) -### 复习 * 3 - -[乘积最大子数组-152](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/18) - -[最大子序和-53](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/17) - ### 复习 * 2 [打家劫舍 - 198](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/10) From ee7eacb6f7bb18c3e714099fed3e0f2bdeb6291c Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Fri, 26 Jun 2020 14:44:39 +0800 Subject: [PATCH 118/145] Update README.md --- README.md | 275 ++++++++++++++++++++++++++---------------------------- 1 file changed, 133 insertions(+), 142 deletions(-) diff --git a/README.md b/README.md index 0e2a61d..a61690a 100644 --- a/README.md +++ b/README.md @@ -16,310 +16,301 @@ 思路会记录在本仓库的 Issues 中,按照 label 进行分类。比如想查看 「DFS」 分类下的问题,那么选择标签进行筛选即可。 ## 目录 - ### 链表 -[移除链表元素-203](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/97) +[移除链表元素-203](https://github.com/sl1673495/leetcode-javascript/issues/97) -[两数相加-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/94) +[两数相加-3](https://github.com/sl1673495/leetcode-javascript/issues/94) -[两两交换链表中的节点-24](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/51) +[两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) -[删除链表的倒数第N个节点-19](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/46) +[删除链表的倒数第N个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) -[删除链表的节点-面试题18](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/40) +[删除链表的节点-面试题18](https://github.com/sl1673495/leetcode-javascript/issues/40) -[反转链表II-92](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/39) +[反转链表II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) -[反转链表 206](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/38) +[反转链表 206](https://github.com/sl1673495/leetcode-javascript/issues/38) ### 双指针 -[搜索二维矩阵 II-240](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/96) +[搜索二维矩阵 II-240](https://github.com/sl1673495/leetcode-javascript/issues/96) -[判断子序列-392](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/89) +[判断子序列-392](https://github.com/sl1673495/leetcode-javascript/issues/89) -[分发饼干-455](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/88) +[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[验证回文串-125](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/33) +[验证回文串-125](https://github.com/sl1673495/leetcode-javascript/issues/33) -[两数之和 II - 输入有序数组-167](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/32) +[两数之和 II - 输入有序数组-167](https://github.com/sl1673495/leetcode-javascript/issues/32) -[合并两个有序数组-88](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/29) +[合并两个有序数组-88](https://github.com/sl1673495/leetcode-javascript/issues/29) -[移动零-283](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/26) +[移动零-283](https://github.com/sl1673495/leetcode-javascript/issues/26) -[删除排序数组中的重复项-26](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/8) +[删除排序数组中的重复项-26](https://github.com/sl1673495/leetcode-javascript/issues/8) -[盛水最多的容器-11](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/3) +[盛水最多的容器-11](https://github.com/sl1673495/leetcode-javascript/issues/3) ### 动态规划 -[单词拆分 II-140](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/95) +[单词拆分 II-140](https://github.com/sl1673495/leetcode-javascript/issues/95) -[单词拆分-139](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/93) +[单词拆分-139](https://github.com/sl1673495/leetcode-javascript/issues/93) -[最长回文子串-5](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/91) +[最长回文子串-5](https://github.com/sl1673495/leetcode-javascript/issues/91) -[无重叠区间-435](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/90) +[无重叠区间-435](https://github.com/sl1673495/leetcode-javascript/issues/90) -[目标和-494](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/87) +[目标和-494](https://github.com/sl1673495/leetcode-javascript/issues/87) -[一和零-474](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/86) +[一和零-474](https://github.com/sl1673495/leetcode-javascript/issues/86) -[最长公共子序列-1143](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/85) +[最长公共子序列-1143](https://github.com/sl1673495/leetcode-javascript/issues/85) -[摆动序列-376](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/84) +[摆动序列-376](https://github.com/sl1673495/leetcode-javascript/issues/84) -[最长上升子序列-300](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/83) +[最长上升子序列-300](https://github.com/sl1673495/leetcode-javascript/issues/83) -[最长等差数列-1027](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/82) +[最长等差数列-1027](https://github.com/sl1673495/leetcode-javascript/issues/82) -[解码方法-91](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/81) +[解码方法-91](https://github.com/sl1673495/leetcode-javascript/issues/81) -[三角形最小路径和-120](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/80) +[三角形最小路径和-120](https://github.com/sl1673495/leetcode-javascript/issues/80) -[最小路径和-64](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/34) +[最小路径和-64](https://github.com/sl1673495/leetcode-javascript/issues/34) -[括号生成-22](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/31) +[括号生成-22](https://github.com/sl1673495/leetcode-javascript/issues/31) -[爬楼梯-70](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/22) +[爬楼梯-70](https://github.com/sl1673495/leetcode-javascript/issues/22) -[买卖股票的最佳时机-121](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/19) +[买卖股票的最佳时机-121](https://github.com/sl1673495/leetcode-javascript/issues/19) -[乘积最大子数组-152](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/18) +[乘积最大子数组-152](https://github.com/sl1673495/leetcode-javascript/issues/18) -[最大子序和-53](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/17) +[最大子序和-53](https://github.com/sl1673495/leetcode-javascript/issues/17) -[分割等和子集(01背包的变种)-416](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/16) +[分割等和子集(01背包的变种)-416](https://github.com/sl1673495/leetcode-javascript/issues/16) -[背包(01背包)](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/15) +[背包(01背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) -[使用最小花费爬楼梯-746](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/13) +[使用最小花费爬楼梯-746](https://github.com/sl1673495/leetcode-javascript/issues/13) -[零钱兑换 II-518](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/12) +[零钱兑换 II-518](https://github.com/sl1673495/leetcode-javascript/issues/12) -[打家劫舍 - 198](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/10) +[打家劫舍 - 198](https://github.com/sl1673495/leetcode-javascript/issues/10) -[完全平方数-279](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/9) +[完全平方数-279](https://github.com/sl1673495/leetcode-javascript/issues/9) -[整数拆分-343](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/7) +[整数拆分-343](https://github.com/sl1673495/leetcode-javascript/issues/7) -[斐波那契数-509](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/2) +[斐波那契数-509](https://github.com/sl1673495/leetcode-javascript/issues/2) ### 递归与回溯 -[单词搜索 II-212](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/92) +[单词搜索 II-212](https://github.com/sl1673495/leetcode-javascript/issues/92) -[解数独-37](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/79) +[解数独-37](https://github.com/sl1673495/leetcode-javascript/issues/79) -[N皇后-51](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/78) +[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) -[单词搜索-79](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/77) +[单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) -[二进制手表-401](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/76) +[二进制手表-401](https://github.com/sl1673495/leetcode-javascript/issues/76) -[子集 II-90](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/75) +[子集 II-90](https://github.com/sl1673495/leetcode-javascript/issues/75) -[ 组合总和 III-216](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/74) +[ 组合总和 III-216](https://github.com/sl1673495/leetcode-javascript/issues/74) -[组合总和 II-40](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/73) +[组合总和 II-40](https://github.com/sl1673495/leetcode-javascript/issues/73) -[组合总和-39](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/72) +[组合总和-39](https://github.com/sl1673495/leetcode-javascript/issues/72) -[子集-78](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/71) +[子集-78](https://github.com/sl1673495/leetcode-javascript/issues/71) -[组合-77](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/70) +[组合-77](https://github.com/sl1673495/leetcode-javascript/issues/70) -[全排列 II-47](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/69) +[全排列 II-47](https://github.com/sl1673495/leetcode-javascript/issues/69) -[全排列-46](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/68) +[全排列-46](https://github.com/sl1673495/leetcode-javascript/issues/68) -[分割回文串-131](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/67) +[分割回文串-131](https://github.com/sl1673495/leetcode-javascript/issues/67) -[复原IP地址-93](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/66) +[复原IP地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) -[电话号码的字母组合-17](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/65) +[电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) ### 贪心算法 -[判断子序列-392](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/89) +[判断子序列-392](https://github.com/sl1673495/leetcode-javascript/issues/89) -[分发饼干-455](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/88) +[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[买卖股票的最佳时机 II-122](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/20) +[买卖股票的最佳时机 II-122](https://github.com/sl1673495/leetcode-javascript/issues/20) ### 例题详解 -[分发饼干-455](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/88) +[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[N皇后-51](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/78) +[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) -[单词搜索-79](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/77) +[单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) -[二进制手表-401](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/76) +[二进制手表-401](https://github.com/sl1673495/leetcode-javascript/issues/76) -[电话号码的字母组合-17](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/65) +[电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) -[二叉树的所有路径-257](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/59) +[二叉树的所有路径-257](https://github.com/sl1673495/leetcode-javascript/issues/59) -[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) +[路径总和-112](https://github.com/sl1673495/leetcode-javascript/issues/57) -[两两交换链表中的节点-24](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/51) +[两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) -[无重复字符的最长子串-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/42) +[无重复字符的最长子串-3](https://github.com/sl1673495/leetcode-javascript/issues/42) -[二分查找-704](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/23) +[二分查找-704](https://github.com/sl1673495/leetcode-javascript/issues/23) -[背包(01背包)](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/15) +[背包(01背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) -[盛水最多的容器-11](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/3) +[盛水最多的容器-11](https://github.com/sl1673495/leetcode-javascript/issues/3) -[斐波那契数-509](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/2) +[斐波那契数-509](https://github.com/sl1673495/leetcode-javascript/issues/2) ### DFS -[二叉树的最近公共祖先-236](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/64) +[二叉树的最近公共祖先-236](https://github.com/sl1673495/leetcode-javascript/issues/64) -[将有序数组转换为二叉搜索树](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/63) +[将有序数组转换为二叉搜索树](https://github.com/sl1673495/leetcode-javascript/issues/63) -[删除二叉搜索树中的节点-450](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/62) +[删除二叉搜索树中的节点-450](https://github.com/sl1673495/leetcode-javascript/issues/62) -[路径总和 III-437](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/61) +[路径总和 III-437](https://github.com/sl1673495/leetcode-javascript/issues/61) -[求根到叶子节点数字之和-129](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/60) +[求根到叶子节点数字之和-129](https://github.com/sl1673495/leetcode-javascript/issues/60) -[二叉树的所有路径-257](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/59) +[二叉树的所有路径-257](https://github.com/sl1673495/leetcode-javascript/issues/59) -[左叶子之和-404](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/58) +[左叶子之和-404](https://github.com/sl1673495/leetcode-javascript/issues/58) -[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) +[路径总和-112](https://github.com/sl1673495/leetcode-javascript/issues/57) -[平衡二叉树-110](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/56) +[平衡二叉树-110](https://github.com/sl1673495/leetcode-javascript/issues/56) -[对称二叉树-101](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/55) +[对称二叉树-101](https://github.com/sl1673495/leetcode-javascript/issues/55) -[二叉树的最小深度-111](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/54) +[二叉树的最小深度-111](https://github.com/sl1673495/leetcode-javascript/issues/54) -[二叉树的最大深度-104](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/53) +[二叉树的最大深度-104](https://github.com/sl1673495/leetcode-javascript/issues/53) -[二叉树的层序遍历](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/30) +[二叉树的层序遍历](https://github.com/sl1673495/leetcode-javascript/issues/30) -[路径总和 II-113](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/27) +[路径总和 II-113](https://github.com/sl1673495/leetcode-javascript/issues/27) -[相同的树-100](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/21) +[相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) -[打家劫舍 |||-337](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/11) +[打家劫舍 |||-337](https://github.com/sl1673495/leetcode-javascript/issues/11) -[被围绕的区域-130](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/6) +[被围绕的区域-130](https://github.com/sl1673495/leetcode-javascript/issues/6) -[岛屿的最大面积-695](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/5) +[岛屿的最大面积-695](https://github.com/sl1673495/leetcode-javascript/issues/5) ### 二叉树 -[二叉树的最近公共祖先-236](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/64) +[二叉树的最近公共祖先-236](https://github.com/sl1673495/leetcode-javascript/issues/64) -[将有序数组转换为二叉搜索树](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/63) +[将有序数组转换为二叉搜索树](https://github.com/sl1673495/leetcode-javascript/issues/63) -[删除二叉搜索树中的节点-450](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/62) +[删除二叉搜索树中的节点-450](https://github.com/sl1673495/leetcode-javascript/issues/62) -[路径总和 III-437](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/61) +[路径总和 III-437](https://github.com/sl1673495/leetcode-javascript/issues/61) -[求根到叶子节点数字之和-129](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/60) +[求根到叶子节点数字之和-129](https://github.com/sl1673495/leetcode-javascript/issues/60) -[二叉树的所有路径-257](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/59) +[二叉树的所有路径-257](https://github.com/sl1673495/leetcode-javascript/issues/59) -[左叶子之和-404](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/58) +[左叶子之和-404](https://github.com/sl1673495/leetcode-javascript/issues/58) -[路径总和-112](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/57) +[路径总和-112](https://github.com/sl1673495/leetcode-javascript/issues/57) -[平衡二叉树-110](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/56) +[平衡二叉树-110](https://github.com/sl1673495/leetcode-javascript/issues/56) -[对称二叉树-101](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/55) +[对称二叉树-101](https://github.com/sl1673495/leetcode-javascript/issues/55) -[二叉树的最小深度-111](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/54) +[二叉树的最小深度-111](https://github.com/sl1673495/leetcode-javascript/issues/54) -[二叉树的最大深度-104](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/53) +[二叉树的最大深度-104](https://github.com/sl1673495/leetcode-javascript/issues/53) -[二叉树的右视图-199](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/52) +[二叉树的右视图-199](https://github.com/sl1673495/leetcode-javascript/issues/52) -[二叉树的前序遍历-144](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/50) +[二叉树的前序遍历-144](https://github.com/sl1673495/leetcode-javascript/issues/50) -[二叉树的层序遍历](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/30) +[二叉树的层序遍历](https://github.com/sl1673495/leetcode-javascript/issues/30) -[路径总和 II-113](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/27) +[路径总和 II-113](https://github.com/sl1673495/leetcode-javascript/issues/27) -[相同的树-100](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/21) +[相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) ### BFS -[二叉树的最小深度-111](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/54) +[二叉树的最小深度-111](https://github.com/sl1673495/leetcode-javascript/issues/54) -[二叉树的最大深度-104](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/53) +[二叉树的最大深度-104](https://github.com/sl1673495/leetcode-javascript/issues/53) -[二叉树的右视图-199](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/52) +[二叉树的右视图-199](https://github.com/sl1673495/leetcode-javascript/issues/52) -[二叉树的层序遍历](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/30) +[二叉树的层序遍历](https://github.com/sl1673495/leetcode-javascript/issues/30) -[相同的树-100](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/21) +[相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) -[在每个树行中找最大值-515](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/4) +[在每个树行中找最大值-515](https://github.com/sl1673495/leetcode-javascript/issues/4) ### 栈和队列 -[二叉树的右视图-199](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/52) +[二叉树的右视图-199](https://github.com/sl1673495/leetcode-javascript/issues/52) -[二叉树的前序遍历-144](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/50) +[二叉树的前序遍历-144](https://github.com/sl1673495/leetcode-javascript/issues/50) -[简化路径-71](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/49) +[简化路径-71](https://github.com/sl1673495/leetcode-javascript/issues/49) -[有效的括号-20](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/48) +[有效的括号-20](https://github.com/sl1673495/leetcode-javascript/issues/48) -[逆波兰表达式求值-150](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/47) +[逆波兰表达式求值-150](https://github.com/sl1673495/leetcode-javascript/issues/47) ### 滑动窗口 -[滑动窗口的最大值-239](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/45) +[滑动窗口的最大值-239](https://github.com/sl1673495/leetcode-javascript/issues/45) -[找到字符串中所有字母异位词-438](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/44) +[找到字符串中所有字母异位词-438](https://github.com/sl1673495/leetcode-javascript/issues/44) -[最小覆盖子串-76](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/43) +[最小覆盖子串-76](https://github.com/sl1673495/leetcode-javascript/issues/43) -[无重复字符的最长子串-3](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/42) +[无重复字符的最长子串-3](https://github.com/sl1673495/leetcode-javascript/issues/42) -[长度最小的子数组-209](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/36) +[长度最小的子数组-209](https://github.com/sl1673495/leetcode-javascript/issues/36) ### 排序 -[快速排序](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/41) +[快速排序](https://github.com/sl1673495/leetcode-javascript/issues/41) -[颜色分类-75](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/28) +[颜色分类-75](https://github.com/sl1673495/leetcode-javascript/issues/28) ### 查找表 -[两个数组的交集 II-350](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/37) +[两个数组的交集 II-350](https://github.com/sl1673495/leetcode-javascript/issues/37) ### 数据结构 -[LRU 缓存机制-146](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/35) +[LRU 缓存机制-146](https://github.com/sl1673495/leetcode-javascript/issues/35) -[实现 Trie (前缀树)-208](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/14) +[实现 Trie (前缀树)-208](https://github.com/sl1673495/leetcode-javascript/issues/14) ### 二分查找 -[Pow(x, n)-50](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/25) - -[x 的平方根-69](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/24) - -[二分查找-704](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/23) - -### 复习 * 2 - -[打家劫舍 - 198](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/10) +[Pow(x, n)-50](https://github.com/sl1673495/leetcode-javascript/issues/25) -[整数拆分-343](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/7) +[x 的平方根-69](https://github.com/sl1673495/leetcode-javascript/issues/24) -[在每个树行中找最大值-515](https://api.github.com/repos/sl1673495/leetcode-javascript/issues/4) +[二分查找-704](https://github.com/sl1673495/leetcode-javascript/issues/23) ## Author From 25bebe355a1f750b330882a9ca1514ca205b2ef4 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 26 Jun 2020 19:11:23 +0800 Subject: [PATCH 119/145] =?UTF-8?q?feat:=20=E6=9C=80=E9=95=BF=E5=8D=95?= =?UTF-8?q?=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\235\242\350\257\225\351\242\230 17.15.js" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\215\225\350\257\215-\351\235\242\350\257\225\351\242\230 17.15.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\215\225\350\257\215-\351\235\242\350\257\225\351\242\230 17.15.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\215\225\350\257\215-\351\235\242\350\257\225\351\242\230 17.15.js" new file mode 100644 index 0000000..fe82f42 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\345\215\225\350\257\215-\351\235\242\350\257\225\351\242\230 17.15.js" @@ -0,0 +1,49 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +let wordBreak = function (s, wordDict) { + let n = s.length + if (!n) return true + + let wordSet = new Set(wordDict) + let dp = [] + dp[0] = true + + for (let i = 0; i <= n; i++) { + for (let j = i; j >= 0; j--) { + let word = s.slice(j, i) + if (wordSet.has(word) && dp[j]) { + dp[i] = true + break + } + } + } + + return !!dp[n] +} +/** + * @param {string[]} words + * @return {string} + */ +let longestWord = function (words) { + // 先长度降序 后字典序升序 排序 + words.sort((a, b) => { + let diff = b.length - a.length + if (diff !== 0) { + return diff + } else { + return a < b ? -1 : 1 + } + }) + words = Array.from(new Set(words)) + for (let i = 0; i < words.length; i++) { + let word = words[i] + let rest = words.slice(0, i).concat(words.slice(i + 1)) + if (wordBreak(word, rest)) { + return word + } + } + return "" +} \ No newline at end of file From 31d2d4ea14864cf33012a0f7cc45df0c388afba4 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 26 Jun 2020 19:12:15 +0800 Subject: [PATCH 120/145] =?UTF-8?q?feat:=20=E9=80=9A=E8=BF=87=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E6=AF=8D=E5=8C=B9=E9=85=8D=E5=88=B0=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E9=87=8C=E6=9C=80=E9=95=BF=E5=8D=95=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...51\225\277\345\215\225\350\257\215-524.js" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "\345\217\214\346\214\207\351\222\210/\351\200\232\350\277\207\345\210\240\351\231\244\345\255\227\346\257\215\345\214\271\351\205\215\345\210\260\345\255\227\345\205\270\351\207\214\346\234\200\351\225\277\345\215\225\350\257\215-524.js" diff --git "a/\345\217\214\346\214\207\351\222\210/\351\200\232\350\277\207\345\210\240\351\231\244\345\255\227\346\257\215\345\214\271\351\205\215\345\210\260\345\255\227\345\205\270\351\207\214\346\234\200\351\225\277\345\215\225\350\257\215-524.js" "b/\345\217\214\346\214\207\351\222\210/\351\200\232\350\277\207\345\210\240\351\231\244\345\255\227\346\257\215\345\214\271\351\205\215\345\210\260\345\255\227\345\205\270\351\207\214\346\234\200\351\225\277\345\215\225\350\257\215-524.js" new file mode 100644 index 0000000..1da6eb0 --- /dev/null +++ "b/\345\217\214\346\214\207\351\222\210/\351\200\232\350\277\207\345\210\240\351\231\244\345\255\227\346\257\215\345\214\271\351\205\215\345\210\260\345\255\227\345\205\270\351\207\214\346\234\200\351\225\277\345\215\225\350\257\215-524.js" @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @param {string[]} d + * @return {string} + */ +let findLongestWord = function (s, d) { + let n = d.length + let points = Array(n).fill(-1) + + let find = "" + for (let i = 0; i < s.length; i++) { + let char = s[i] + for (let j = 0; j < n; j++) { + let targetChar = d[j][points[j] + 1] + if (char === targetChar) { + points[j]++ + let word = d[j] + let wl = d[j].length + if (points[j] === wl - 1) { + let fl = find.length + if (wl > fl || (wl === fl && word < find)) { + find = word + } + } + } + } + } + + return find +} \ No newline at end of file From 16fa5cc54456ce449382d60581f8f2779180a4d6 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Fri, 26 Jun 2020 19:43:04 +0800 Subject: [PATCH 121/145] =?UTF-8?q?feat:=20=E6=81=A2=E5=A4=8D=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\235\242\350\257\225\351\242\230 17.13.js" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" new file mode 100644 index 0000000..a76fea4 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" @@ -0,0 +1,19 @@ +/** + * @param {string[]} dictionary + * @param {string} sentence + * @return {number} + */ +let respace = function (dictionary, sentence) { + let n = sentence.length + let dp = [0] + for (let i = 1; i <= n; i++) { + let min = dp[i - 1] + 1 + for (let word of dictionary) { + if (sentence.substring(i - word.length, i) === word) { + min = Math.min(min, dp[i - word.length]) + } + } + dp[i] = min + } + return dp[n] +} \ No newline at end of file From f95e194e32100c1a2d0e22481fcecaeb6c3a16db Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 27 Jun 2020 01:52:29 +0800 Subject: [PATCH 122/145] =?UTF-8?q?feat:=20=E6=9C=80=E5=A4=A7=E6=AD=A3?= =?UTF-8?q?=E6=96=B9=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\235\242\350\257\225\351\242\230 17.13.js" | 2 +- ...46\255\243\346\226\271\345\275\242-221.js" | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242-221.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" index a76fea4..676d151 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\201\242\345\244\215\347\251\272\346\240\274-\351\235\242\350\257\225\351\242\230 17.13.js" @@ -16,4 +16,4 @@ let respace = function (dictionary, sentence) { dp[i] = min } return dp[n] -} \ No newline at end of file +} diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242-221.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242-221.js" new file mode 100644 index 0000000..26c4815 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242-221.js" @@ -0,0 +1,44 @@ +/** + * @param {character[][]} matrix + * @return {number} + */ +let maximalSquare = function (matrix) { + let maxY = matrix.length + if (!maxY) return 0 + let maxX = matrix[0].length + + let dp = [] + let max = 0 + + let dpBasic = (y, x) => { + if (matrix[y][x] === "1") { + max = 1 + dp[y][x] = 1 + } else { + dp[y][x] = 0 + } + } + for (let y = 0; y < maxY; y++) { + dp[y] = [] + dpBasic(y, 0) + } + for (let x = 1; x < maxX; x++) { + dpBasic(0, x) + } + + for (let y = 1; y < maxY; y++) { + for (let x = 1; x < maxX; x++) { + let val = matrix[y][x] + if (val === "0") { + dp[y][x] = 0 + } else { + let left = dp[y][x - 1] + let top = dp[y - 1][x] + let leftTop = dp[y - 1][x - 1] + dp[y][x] = Math.min(left, top, leftTop) + 1 + max = Math.max(max, dp[y][x]) + } + } + } + return max * max +} From 3698d2caf2189430020ddef5efdbff7b5df6cb50 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 27 Jun 2020 13:26:09 +0800 Subject: [PATCH 123/145] =?UTF-8?q?feat:=20=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=20III?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\203\346\270\270\346\210\217 III-1306.js" | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 "BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" diff --git "a/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" new file mode 100644 index 0000000..73d910e --- /dev/null +++ "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" @@ -0,0 +1,27 @@ +/** + * @param {number[]} arr + * @param {number} start + * @return {boolean} + */ +let canReach = function (arr, start) { + let n = arr.length + let visited = [] + let queue = [start] + while (queue.length) { + let index = queue.pop() + let val = arr[index] + if (val === 0) { + return true + } + let left = index - val + let right = index + val + if (left >= 0 && !visited[left]) { + queue.push(left) + } + if (right < n && !visited[right]) { + queue.push(right) + } + visited[index] = true + } + return false +}; \ No newline at end of file From 1bf7e028caf19b8c7a5ce91c0686cbc91f6fae93 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 27 Jun 2020 14:27:55 +0800 Subject: [PATCH 124/145] =?UTF-8?q?feat:=20=E8=B7=B3=E8=B7=83=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=20IIII?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...67\203\346\270\270\346\210\217 IV-1345.js" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" diff --git "a/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" new file mode 100644 index 0000000..be115b2 --- /dev/null +++ "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" @@ -0,0 +1,72 @@ +/** + * @param {number[]} arr + * @return {number} + */ +let minJumps = function (arr) { + let n = arr.length + if (n === 1) { + return 0 + } + + // 连续出现超过两次的数字就抛弃掉 + let newArr = [] + let sameCount = 0 + for (let i = 0; i < arr.length; i++) { + if (arr[i] === arr[i - 1]) { + sameCount += 1 + if (sameCount >= 2) { + continue + } else { + newArr.push(arr[i]) + } + } else { + newArr.push(arr[i]) + sameCount = 0 + } + } + arr = newArr + n = arr.length + // 遍历一遍 记录每个数字出现的下标位置 + let indexesMap = new Map() + for (let i = 0; i < n; i++) { + let val = arr[i] + let indexes = indexesMap.get(val) + if (!indexes) { + indexesMap.set(val, [i]) + } else { + indexes.push(i) + } + } + + let visited = [] + let count = 0 + let queue = [0] + while (queue.length) { + count++ + let len = queue.length + for (let i = 0; i < len; i++) { + let index = queue.shift() + // 找到了 由于bfs的特性 此时用的跳跃次数一定是最少的 + if (index === n - 1) { + return count - 1 + } + + // 没找到 继续把可以跳的几个位置都放入队列中 + let val = arr[index] + let left = index - 1 + let right = index + 1 + let sameIndexes = indexesMap.get(val) + + if (left >= 0 && !visited[left]) queue.push(left) + if (right < n && !visited[right]) queue.push(right) + for (let sameIndex of sameIndexes) { + if (sameIndex !== index && !visited[sameIndex]) { + queue.push(sameIndex) + } + } + + visited[index] = true + } + } + return n +} From 139997af166bff9fbf1d71795c8ae42ac1544162 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 27 Jun 2020 19:12:22 +0800 Subject: [PATCH 125/145] =?UTF-8?q?feat:=20=E9=BB=84=E9=87=91=E7=9F=BF?= =?UTF-8?q?=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\207\221\347\237\277\345\267\245-1219.js" | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" new file mode 100644 index 0000000..0d8d53c --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" @@ -0,0 +1,69 @@ +/** + * @param {number[][]} grid + * @return {number} + */ +let getMaximumGold = function (grid) { + let maxY = grid.length + if (maxY === 0) { + return 0 + } + let maxX = grid[0].length + + let visited = [] + for (let y = 0; y < maxY; y++) { + visited[y] = [] + } + + let dirs = [ + [1, 0], + [-1, 0], + [0, -1], + [0, 1], + ] + + // 验证是否能走入这个格子 + // 1. 范围不能越界 + // 2. 本轮递归中未访问过 + // 3. 格子的值不能为 0 + let isValid = (y, x) => { + return ( + y >= 0 && + y < maxY && + x >= 0 && + x < maxX && + grid[y][x] !== 0 && + !visited[y][x] + ) + } + + let maxGold = 0 + let helper = (y, x, prevGold) => { + let val = grid[y][x] + let curGold = prevGold + val + if (curGold === 0) { + return + } + + for (let dir of dirs) { + let [diffY, diffX] = dir + let nextY = y + diffY + let nextX = x + diffX + if (isValid(nextY, nextX)) { + visited[y][x] = true + helper(nextY, nextX, curGold) + visited[y][x] = false + } else { + // 走到尽头或者不符合条件的了 + maxGold = Math.max(maxGold, curGold) + } + } + } + + for (let y = 0; y < maxY; y++) { + for (let x = 0; x < maxX; x++) { + helper(y, x, 0) + } + } + + return maxGold +} From cc700b8f12ff4e900b4442658e29f3ce758a0ced Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 27 Jun 2020 22:45:11 +0800 Subject: [PATCH 126/145] =?UTF-8?q?feat:=20=E6=B4=BB=E5=AD=97=E5=8D=B0?= =?UTF-8?q?=E5=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\255\227\345\215\260\345\210\267-1079.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\264\273\345\255\227\345\215\260\345\210\267-1079.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\264\273\345\255\227\345\215\260\345\210\267-1079.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\264\273\345\255\227\345\215\260\345\210\267-1079.js" new file mode 100644 index 0000000..40ff332 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\264\273\345\255\227\345\215\260\345\210\267-1079.js" @@ -0,0 +1,23 @@ +/** + * @param {string} tiles + * @return {number} + */ +let numTilePossibilities = function (tiles) { + let res = new Set() + + let helper = (prev, rest) => { + if (prev.length > 0) { + res.add(prev) + } + + for (let i = 0; i < rest.length; i++) { + let char = rest[i] + let cur = prev + char + helper(cur, rest.substring(0, i) + rest.substring(i + 1)) + } + } + + helper('', tiles) + + return res.size +}; \ No newline at end of file From 45e87ab6a3894dad8a5e3cb4d7f8791f1196bd7d Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 27 Jun 2020 23:00:50 +0800 Subject: [PATCH 127/145] =?UTF-8?q?feat:=20=E5=AD=97=E6=AF=8D=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E5=86=99=E5=85=A8=E6=8E=92=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\205\250\346\216\222\345\210\227-784.js" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\345\244\247\345\260\217\345\206\231\345\205\250\346\216\222\345\210\227-784.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\345\244\247\345\260\217\345\206\231\345\205\250\346\216\222\345\210\227-784.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\345\244\247\345\260\217\345\206\231\345\205\250\346\216\222\345\210\227-784.js" new file mode 100644 index 0000000..91ba7b7 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\345\244\247\345\260\217\345\206\231\345\205\250\346\216\222\345\210\227-784.js" @@ -0,0 +1,34 @@ +/** + * @param {string} S + * @return {string[]} + */ +let letterCasePermutation = function (S) { + let res = [] + + let helper = (prev, rest) => { + if (prev.length === S.length) { + res.push(prev) + return + } + + let char = rest[0] + let word1 = prev + char + let nextRest = rest.substring(1) + + if (!isNaN(Number(char))) { + helper(word1, nextRest) + return + } else { + let upperChar = char.toUpperCase() + let char2 = upperChar === char ? char.toLowerCase() : upperChar + let word2 = prev + char2 + + helper(word1, nextRest) + helper(word2, nextRest) + } + } + + helper('', S) + + return res +}; \ No newline at end of file From 4f1565c231de33f607f881c7abe1a98fe3861370 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 29 Jun 2020 13:56:25 +0800 Subject: [PATCH 128/145] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=9B=9E?= =?UTF-8?q?=E6=BA=AF=E5=92=8Cdp=E4=B8=A4=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\234\200\345\260\217\345\222\214-931.js" | 34 +++++++++ ...20\214\350\267\257\345\276\204 III-980.js" | 72 +++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\270\215\345\220\214\350\267\257\345\276\204 III-980.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" new file mode 100644 index 0000000..30ef633 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" @@ -0,0 +1,34 @@ +/** + * @param {number[][]} A + * @return {number} + */ +let minFallingPathSum = function (A) { + let n = A.length + + let dp = [] + for (let i = 0; i < n; i++) { + dp[i] = [] + } + + for (let j = 0; j < n; j++) { + dp[n - 1][j] = A[n - 1][j] + } + + for (let i = n - 2; i >= 0; i--) { + for (let j = 0; j < n; j++) { + dp[i][j] = Infinity + let left = j - 1 + let right = j + 1 + let mid = j + let nextRowIndexes = [left, mid, right] + for (let nextRowIndex of nextRowIndexes) { + if (nextRowIndex >= 0 && nextRowIndex < n) { + dp[i][j] = Math.min(dp[i][j], A[i][j] + dp[i + 1][nextRowIndex]) + } + } + } + } + + // 第一行的最小值 可以确定整体的最小路径 + return Math.min(...dp[0]) +} diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\270\215\345\220\214\350\267\257\345\276\204 III-980.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\270\215\345\220\214\350\267\257\345\276\204 III-980.js" new file mode 100644 index 0000000..1213047 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\270\215\345\220\214\350\267\257\345\276\204 III-980.js" @@ -0,0 +1,72 @@ +/** + * @param {number[][]} grid + * @return {number} + */ +let uniquePathsIII = function (grid) { + let maxY = grid.length + if (!maxY) return 0 + let maxX = grid[0].length + + let validCellsCount = 0 + let entry + let visited = [] + for (let y = 0; y < maxY; y++) { + visited[y] = [] + for (let x = 0; x < maxX; x++) { + let val = grid[y][x] + if (val === 0) { + validCellsCount++ + } + if (val === 1) { + entry = [y, x] + } + } + } + + let isValid = (y, x) => { + return ( + y >= 0 && + y < maxY && + x >= 0 && + x < maxX && + !visited[y][x] && + grid[y][x] !== -1 + ) + } + + let dirs = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], + ] + let res = 0 + + let dfs = (y, x, passCount) => { + let val = grid[y][x] + if (val === 2) { + if (passCount === validCellsCount) { + res++ + } + return + } else if (val === 0) { + passCount += 1 + } + + for (let [diffY, diffX] of dirs) { + let nextY = y + diffY + let nextX = x + diffX + if (isValid(nextY, nextX)) { + visited[nextY][nextX] = true + dfs(nextY, nextX, passCount) + visited[nextY][nextX] = false + } + } + } + + let [entryY, entryX] = entry + visited[entryY][entryX] = true + dfs(entryY, entryX, 0) + + return res +} From dba403a2d2695f0978ab12dafb0e35f7ba770f52 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 29 Jun 2020 14:22:29 +0800 Subject: [PATCH 129/145] =?UTF-8?q?feat:=20=E6=89=BE=E4=B8=8D=E5=90=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\211\276\344\270\215\345\220\214-389.js" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 "\344\275\215\350\277\220\347\256\227/\346\211\276\344\270\215\345\220\214-389.js" diff --git "a/\344\275\215\350\277\220\347\256\227/\346\211\276\344\270\215\345\220\214-389.js" "b/\344\275\215\350\277\220\347\256\227/\346\211\276\344\270\215\345\220\214-389.js" new file mode 100644 index 0000000..f21e453 --- /dev/null +++ "b/\344\275\215\350\277\220\347\256\227/\346\211\276\344\270\215\345\220\214-389.js" @@ -0,0 +1,15 @@ +/** + * @param {string} s + * @param {string} t + * @return {character} + */ +let findTheDifference = function (s, t) { + let rest = t.charCodeAt(t.length - 1) + for (let i = 0; i < s.length; i++) { + let charS = s[i] + let charT = t[i] + rest ^= charS.charCodeAt(0) + rest ^= charT.charCodeAt(0) + } + return String.fromCharCode(rest) +} From 1730cce4f4f5a4fe7b465f4b8dfdc03405b2543f Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Mon, 29 Jun 2020 22:38:12 +0800 Subject: [PATCH 130/145] =?UTF-8?q?feat:=20=E7=9F=A9=E9=98=B5=E7=BD=AE?= =?UTF-8?q?=E9=9B=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\255\220\346\225\260\347\273\204-560.js" | 23 ++++++++++++ ...351\230\265\347\275\256\351\233\266-73.js" | 37 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\237\251\351\230\265\347\275\256\351\233\266-73.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" new file mode 100644 index 0000000..25582b5 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +let subarraySum = function (nums, k) { + let n = nums.length + if (!n) { + return 0 + } + + let res = 0 + for (let i = 0; i < n; i++) { + let total = 0 + for (let j = i; j < n; j++) { + total += nums[j] + if (total === k) { + res += 1 + } + } + } + return res +}; \ No newline at end of file diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\237\251\351\230\265\347\275\256\351\233\266-73.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\237\251\351\230\265\347\275\256\351\233\266-73.js" new file mode 100644 index 0000000..fff9854 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\347\237\251\351\230\265\347\275\256\351\233\266-73.js" @@ -0,0 +1,37 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +let setZeroes = function (matrix) { + let maxY = matrix.length + if (!maxY) return + let maxX = matrix[0].length + + let handledRows = [] + let handledColumns = [] + + let zeros = [] + for (let y = 0; y < maxY; y++) { + for (let x = 0; x < maxX; x++) { + let val = matrix[y][x] + if (val === 0) { + zeros.push([y, x]) + } + } + } + + for (let [y, x] of zeros) { + if (!handledRows[x]) { + for (let i = 0; i < maxY; i++) { + matrix[i][x] = 0 + } + handledRows[x] = true + } + if (!handledColumns[y]) { + for (let i = 0; i < maxX; i++) { + matrix[y][i] = 0 + } + handledColumns[y] = true + } + } +}; \ No newline at end of file From 9781111a97f969709635092d2f056374cc66acda Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Tue, 30 Jun 2020 18:36:35 +0800 Subject: [PATCH 131/145] =?UTF-8?q?feat:=20=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\227\213\347\237\251\351\230\265-54.js" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\236\272\346\227\213\347\237\251\351\230\265-54.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\236\272\346\227\213\347\237\251\351\230\265-54.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\236\272\346\227\213\347\237\251\351\230\265-54.js" new file mode 100644 index 0000000..5b64a29 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\236\272\346\227\213\347\237\251\351\230\265-54.js" @@ -0,0 +1,56 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + */ +let spiralOrder = function (matrix) { + let maxY = matrix.length + if (!maxY) return [] + let maxX = matrix[0].length + + // 记录一个 visited 数组 + // 按照 右 -> 下 -> 左 -> 上 的方向不断前进 + // 直到遇到边界或者已经访问过的元素 则切换成下一个方向 + let dirs = [ + [0, 1], // 右 + [1, 0], // 下 + [0, -1], // 左 + [-1, 0], // 上 + ] + + let currentDirIndex = 0 + + let visited = [] + for (let y = 0; y < maxY; y++) { + visited[y] = [] + } + let isValid = (y, x) => { + return y >= 0 && y < maxY && x >= 0 && x < maxX && !visited[y][x] + } + + let targetLength = maxY * maxX + let res = [] + + let helper = (y, x) => { + let val = matrix[y][x] + res.push(val) + + if (res.length === targetLength) { + return res + } + + visited[y][x] = true + let [diffY, diffX] = dirs[currentDirIndex % 4] + let nextY = y + diffY + let nextX = x + diffX + if (!isValid(nextY, nextX)) { + [diffY, diffX] = dirs[++currentDirIndex % 4] + nextY = y + diffY + nextX = x + diffX + } + helper(nextY, nextX) + } + + helper(0, 0) + + return res +}; \ No newline at end of file From e0a5640a04f9f71b3fba5799b767598582ee9393 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Tue, 30 Jun 2020 18:56:38 +0800 Subject: [PATCH 132/145] =?UTF-8?q?feat:=20=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...345\275\222\347\237\251\351\230\265-59.js" | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\200\222\345\275\222\347\237\251\351\230\265-59.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\200\222\345\275\222\347\237\251\351\230\265-59.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\200\222\345\275\222\347\237\251\351\230\265-59.js" new file mode 100644 index 0000000..0be2940 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\200\222\345\275\222\347\237\251\351\230\265-59.js" @@ -0,0 +1,54 @@ +/** + * @param {number} n + * @return {number[][]} + */ +let generateMatrix = function (n) { + // 记录一个 visited 数组 + // 按照 右 -> 下 -> 左 -> 上 的方向不断前进 + // 直到遇到边界或者已经访问过的元素 则切换成下一个方向 + let dirs = [ + [0, 1], // 右 + [1, 0], // 下 + [0, -1], // 左 + [-1, 0], // 上 + ] + + let currentDirIndex = 0 + + let visited = [] + for (let y = 0; y < n; y++) { + visited[y] = [] + } + let isValid = (y, x) => { + return y >= 0 && y < n && x >= 0 && x < n && !visited[y][x] + } + + let targetLength = n * n + let res = [] + for (let y = 0; y < n; y++) { + res[y] = [] + } + + let helper = (y, x, num) => { + res[y][x] = num + + if (num === targetLength) { + return res + } + + visited[y][x] = true + let [diffY, diffX] = dirs[currentDirIndex % 4] + let nextY = y + diffY + let nextX = x + diffX + if (!isValid(nextY, nextX)) { + ;[diffY, diffX] = dirs[++currentDirIndex % 4] + nextY = y + diffY + nextX = x + diffX + } + helper(nextY, nextX, num + 1) + } + + helper(0, 0, 1) + + return res +} From 5ddd7703d145bb811388599d6b470d348f82de91 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Wed, 1 Jul 2020 00:34:36 +0800 Subject: [PATCH 133/145] =?UTF-8?q?feat:=20=E6=9C=80=E9=95=BF=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=AD=90=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\255\220\346\225\260\347\273\204-718.js" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204-718.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204-718.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204-718.js" new file mode 100644 index 0000000..a4dfc61 --- /dev/null +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\346\225\260\347\273\204-718.js" @@ -0,0 +1,33 @@ +/** + * @param {number[]} A + * @param {number[]} B + * @return {number} + */ +let findLength = function (A, B) { + let dp = [] + let al = A.length + let bl = B.length + + for (let i = 0; i <= al; i++) { + dp[i] = [] + for (let j = 0; j <= bl; j++) { + dp[i][j] = 0 + } + } + + let max = 0 + for (let i = al - 1; i >= 0; i--) { + for (let j = bl - 1; j >= 0; j--) { + let a = A[i] + let b = B[j] + + if (a === b) { + dp[i][j] = dp[i + 1][j + 1] + 1 + max = Math.max(max, dp[i][j]) + } else { + dp[i][j] = 0 + } + } + } + return max +} From 8e35d3b4581200dd36f008973a02d268a3684f10 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Wed, 1 Jul 2020 02:30:25 +0800 Subject: [PATCH 134/145] =?UTF-8?q?feat:=20=E6=9C=80=E6=8E=A5=E8=BF=91?= =?UTF-8?q?=E7=9A=84=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\225\260\344\271\213\345\222\214-16.js" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "\345\217\214\346\214\207\351\222\210/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214-16.js" diff --git "a/\345\217\214\346\214\207\351\222\210/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214-16.js" "b/\345\217\214\346\214\207\351\222\210/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214-16.js" new file mode 100644 index 0000000..70d7631 --- /dev/null +++ "b/\345\217\214\346\214\207\351\222\210/\346\234\200\346\216\245\350\277\221\347\232\204\344\270\211\346\225\260\344\271\213\345\222\214-16.js" @@ -0,0 +1,49 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +let threeSumClosest = function (nums, target) { + let n = nums.length + if (n === 3) { + return getSum(nums) + } + // 先升序排序 此为解题的前置条件 + nums.sort((a, b) => a - b) + + let min = Infinity // 和 target 的最小差 + let res + + // 从左往右依次尝试定一个基础指针 右边至少再保留两位 否则无法凑成3个 + for (let i = 0; i <= nums.length - 3; i++) { + let basic = nums[i] + let left = i + 1 // 左指针先从 i 右侧的第一位开始尝试 + let right = n - 1 // 右指针先从数组最后一项开始尝试 + + while (left < right) { + let sum = basic + nums[left] + nums[right] // 三数求和 + // 更新最小差 + let diff = Math.abs(sum - target) + if (diff < min) { + min = diff + res = sum + } + if (sum < target) { + // 求出的和如果小于目标值的话 可以尝试把左指针右移 扩大值 + left++ + } else if (sum > target) { + // 反之则右指针左移 + right-- + } else { + // 相等的话 差就为0 一定是答案 + return sum + } + } + } + + return res +} + +function getSum(nums) { + return nums.reduce((total, cur) => total + cur, 0) +} From 17a4ea3ea7d526126eda6e8cd8864ca42a903484 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Thu, 2 Jul 2020 21:21:12 +0800 Subject: [PATCH 135/145] =?UTF-8?q?feat:=20=E6=9C=89=E5=BA=8F=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E4=B8=AD=E7=AC=ACK=E5=B0=8F=E7=9A=84=E5=85=83?= =?UTF-8?q?=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...47\232\204\345\205\203\347\264\240-378.js" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 "\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240-378.js" diff --git "a/\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240-378.js" "b/\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240-378.js" new file mode 100644 index 0000000..6ce08fc --- /dev/null +++ "b/\346\234\211\345\272\217\347\237\251\351\230\265\344\270\255\347\254\254K\345\260\217\347\232\204\345\205\203\347\264\240-378.js" @@ -0,0 +1,31 @@ +/** + * @param {number[][]} matrix + * @param {number} k + * @return {number} + */ +let kthSmallest = function (matrix, k) { + let maxY = matrix.length + let sorted = [] + let points = [] + for (let i = 0; i < maxY; i++) { + points[i] = 0 + } + + while (sorted.length < k) { + let min = Infinity + let minY + for (let y = 0; y < maxY; y++) { + let point = points[y] + let num = matrix[y][point] + if (num < min) { + min = num + minY = y + } + } + sorted.push(min) + // 选中最小项的x指针右移 + points[minY]++ + } + + return sorted[sorted.length - 1] +}; \ No newline at end of file From 6064152a745b263cc0b68a799e29dd8ff42be72b Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 4 Jul 2020 12:40:30 +0800 Subject: [PATCH 136/145] =?UTF-8?q?feat:=20=E5=AD=97=E6=AF=8D=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E7=BB=84=E5=90=88=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\273\204\345\220\210\345\231\250-1286.js" | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\350\277\255\344\273\243\347\273\204\345\220\210\345\231\250-1286.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\350\277\255\344\273\243\347\273\204\345\220\210\345\231\250-1286.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\350\277\255\344\273\243\347\273\204\345\220\210\345\231\250-1286.js" new file mode 100644 index 0000000..709bae1 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\255\227\346\257\215\350\277\255\344\273\243\347\273\204\345\220\210\345\231\250-1286.js" @@ -0,0 +1,52 @@ +/** + * @param {string} characters + * @param {number} combinationLength + */ +let CombinationIterator = function (characters, combinationLength) { + let cl = characters.length + let res = [] + let helper = (start, prev) => { + let pl = prev.length + for (let i = start; i < cl; i++) { + let rest = cl - i + let diff = combinationLength - pl + if (diff > rest) { + return + } + let next = prev + characters[i] + if (next.length == combinationLength) { + res.push(next) + } else { + helper(i + 1, next) + } + } + } + helper(0, "") + this.res = res.sort((a, b) => (a > b ? 1 : -1)) + this.point = 0 +} + +/** + * @return {string} + */ +CombinationIterator.prototype.next = function () { + if (!this.hasNext()) { + return undefined + } + let val = this.res[this.point++] + return val +} + +/** + * @return {boolean} + */ +CombinationIterator.prototype.hasNext = function () { + return this.point !== this.res.length +} + +/** + * Your CombinationIterator object will be instantiated and called as such: + * var obj = new CombinationIterator(characters, combinationLength) + * var param_1 = obj.next() + * var param_2 = obj.hasNext() + */ From 3998c84f22cea30b10a49751dbeff6245b43d48c Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sat, 4 Jul 2020 14:22:18 +0800 Subject: [PATCH 137/145] =?UTF-8?q?feat:=20=E9=A1=BA=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\241\272\346\254\241\346\225\260-1291.js" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\241\272\346\254\241\346\225\260-1291.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\241\272\346\254\241\346\225\260-1291.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\241\272\346\254\241\346\225\260-1291.js" new file mode 100644 index 0000000..13f8a5c --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\241\272\346\254\241\346\225\260-1291.js" @@ -0,0 +1,30 @@ +/** + * @param {number} low + * @param {number} high + * @return {number[]} + */ +let sequentialDigits = function (low, high) { + let lowLen = low.toString().length + let highLen = high.toString().length + let lens = [] + for (let i = lowLen; i <= highLen; i++) { + lens.push(i) + } + + let res = [] + for (let i = 0; i < lens.length; i++) { + let len = lens[i] + for (let start = 1; start <= 10 - len; start++) { + let num = start + + for (let n = start + 1; n < start + len; n++) { + num = 10 * num + n + } + if (num <= high && num >= low) { + res.push(num) + } + } + } + + return res +} \ No newline at end of file From 852f31f67ffe3fa21784d3b93df8e9b73c1cac49 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Sun, 5 Jul 2020 02:13:23 +0800 Subject: [PATCH 138/145] docs: format --- README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a61690a..dfd2737 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ 思路会记录在本仓库的 Issues 中,按照 label 进行分类。比如想查看 「DFS」 分类下的问题,那么选择标签进行筛选即可。 ## 目录 + ### 链表 [移除链表元素-203](https://github.com/sl1673495/leetcode-javascript/issues/97) @@ -24,11 +25,11 @@ [两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) -[删除链表的倒数第N个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) +[删除链表的倒数第 N 个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) -[删除链表的节点-面试题18](https://github.com/sl1673495/leetcode-javascript/issues/40) +[删除链表的节点-面试题 18](https://github.com/sl1673495/leetcode-javascript/issues/40) -[反转链表II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) +[反转链表 II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) [反转链表 206](https://github.com/sl1673495/leetcode-javascript/issues/38) @@ -90,9 +91,9 @@ [最大子序和-53](https://github.com/sl1673495/leetcode-javascript/issues/17) -[分割等和子集(01背包的变种)-416](https://github.com/sl1673495/leetcode-javascript/issues/16) +[分割等和子集(01 背包的变种)-416](https://github.com/sl1673495/leetcode-javascript/issues/16) -[背包(01背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) +[背包(01 背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) [使用最小花费爬楼梯-746](https://github.com/sl1673495/leetcode-javascript/issues/13) @@ -112,7 +113,7 @@ [解数独-37](https://github.com/sl1673495/leetcode-javascript/issues/79) -[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) +[N 皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) [单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) @@ -136,7 +137,7 @@ [分割回文串-131](https://github.com/sl1673495/leetcode-javascript/issues/67) -[复原IP地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) +[复原 IP 地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) [电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) @@ -152,7 +153,7 @@ [分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) +[N 皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) [单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) @@ -170,7 +171,7 @@ [二分查找-704](https://github.com/sl1673495/leetcode-javascript/issues/23) -[背包(01背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) +[背包(01 背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) [盛水最多的容器-11](https://github.com/sl1673495/leetcode-javascript/issues/3) From c674be0d0e465fa31a87cf01bb1c3b5e1ef7a80c Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Tue, 7 Jul 2020 16:45:37 +0800 Subject: [PATCH 139/145] =?UTF-8?q?feat:=20=E6=8B=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\235\242\350\257\225\351\242\230 08.09.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\213\254\345\217\267-\351\235\242\350\257\225\351\242\230 08.09.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\213\254\345\217\267-\351\235\242\350\257\225\351\242\230 08.09.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\213\254\345\217\267-\351\235\242\350\257\225\351\242\230 08.09.js" new file mode 100644 index 0000000..604f5c4 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\346\213\254\345\217\267-\351\235\242\350\257\225\351\242\230 08.09.js" @@ -0,0 +1,23 @@ +/** + * @param {number} n + * @return {string[]} + */ +let generateParenthesis = function (n) { + let res = [] + + let helper = (left, right, prev) => { + if (left < 0 || right < 0 || right < left) { + return + } + if (left === 0 && right === 0) { + res.push(prev) + return + } + + helper(left - 1, right, prev + "(") + helper(left, right - 1, prev + ")") + } + + helper(n, n, '') + return res +}; \ No newline at end of file From a5adb8ef868b4ca83b66c0ac03814e84426d6b07 Mon Sep 17 00:00:00 2001 From: sl1673495 <949589855@qq.com> Date: Wed, 8 Jul 2020 23:51:45 +0800 Subject: [PATCH 140/145] =?UTF-8?q?feat:=20=E8=B7=B3=E6=B0=B4=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\235\242\350\257\225\351\242\230 16.11.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\267\263\346\260\264\346\235\277-\351\235\242\350\257\225\351\242\230 16.11.js" diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\267\263\346\260\264\346\235\277-\351\235\242\350\257\225\351\242\230 16.11.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\267\263\346\260\264\346\235\277-\351\235\242\350\257\225\351\242\230 16.11.js" new file mode 100644 index 0000000..fb26019 --- /dev/null +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\350\267\263\346\260\264\346\235\277-\351\235\242\350\257\225\351\242\230 16.11.js" @@ -0,0 +1,23 @@ +/** + * @param {number} shorter + * @param {number} longer + * @param {number} k + * @return {number[]} + */ +let divingBoard = function (shorter, longer, k) { + if (k === 0) { + return [] + } + if (shorter === longer) { + return [k * shorter] + } + + let res = [] + for (let i = 0; i <= k; i++) { + let longCount = i + let shortCount = k - i + res.push(shortCount * shorter + longCount * longer) + } + + return res +}; \ No newline at end of file From d1ad49174b0e7e8be76f7d8269740bc602a1bd06 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 11 Jul 2020 14:00:39 +0800 Subject: [PATCH 141/145] Update README.md --- README.md | 217 +++++++++++++++++++++++++++++------------------------- 1 file changed, 118 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index dfd2737..78e30a6 100644 --- a/README.md +++ b/README.md @@ -17,44 +17,101 @@ ## 目录 -### 链表 -[移除链表元素-203](https://github.com/sl1673495/leetcode-javascript/issues/97) +### 例题详解 -[两数相加-3](https://github.com/sl1673495/leetcode-javascript/issues/94) +[最接近的三数之和-16](https://github.com/sl1673495/leetcode-javascript/issues/115) + +[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) + +[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) + +[单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) + +[二进制手表-401](https://github.com/sl1673495/leetcode-javascript/issues/76) + +[电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) + +[二叉树的所有路径-257](https://github.com/sl1673495/leetcode-javascript/issues/59) + +[路径总和-112](https://github.com/sl1673495/leetcode-javascript/issues/57) [两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) -[删除链表的倒数第 N 个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) +[有效的括号-20](https://github.com/sl1673495/leetcode-javascript/issues/48) + +[无重复字符的最长子串-3](https://github.com/sl1673495/leetcode-javascript/issues/42) -[删除链表的节点-面试题 18](https://github.com/sl1673495/leetcode-javascript/issues/40) +[二分查找-704](https://github.com/sl1673495/leetcode-javascript/issues/23) -[反转链表 II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) +### 递归与回溯 -[反转链表 206](https://github.com/sl1673495/leetcode-javascript/issues/38) +[跳水板-面试题 16.11 ](https://github.com/sl1673495/leetcode-javascript/issues/118) -### 双指针 +[顺次数-1291](https://github.com/sl1673495/leetcode-javascript/issues/116) -[搜索二维矩阵 II-240](https://github.com/sl1673495/leetcode-javascript/issues/96) +[螺旋矩阵 II-59](https://github.com/sl1673495/leetcode-javascript/issues/113) -[判断子序列-392](https://github.com/sl1673495/leetcode-javascript/issues/89) +[螺旋矩阵-54](https://github.com/sl1673495/leetcode-javascript/issues/112) -[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) +[矩阵置零-73](https://github.com/sl1673495/leetcode-javascript/issues/111) -[验证回文串-125](https://github.com/sl1673495/leetcode-javascript/issues/33) +[不同路径 III-980](https://github.com/sl1673495/leetcode-javascript/issues/107) -[两数之和 II - 输入有序数组-167](https://github.com/sl1673495/leetcode-javascript/issues/32) +[字母大小写全排列-784](https://github.com/sl1673495/leetcode-javascript/issues/106) -[合并两个有序数组-88](https://github.com/sl1673495/leetcode-javascript/issues/29) +[黄金矿工-1219](https://github.com/sl1673495/leetcode-javascript/issues/105) -[移动零-283](https://github.com/sl1673495/leetcode-javascript/issues/26) +[有重复字符串的排列组合-面试题 08.08](https://github.com/sl1673495/leetcode-javascript/issues/104) + +[单词搜索 II-212](https://github.com/sl1673495/leetcode-javascript/issues/92) + +[解数独-37](https://github.com/sl1673495/leetcode-javascript/issues/79) + +[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) + +[单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) -[删除排序数组中的重复项-26](https://github.com/sl1673495/leetcode-javascript/issues/8) +[二进制手表-401](https://github.com/sl1673495/leetcode-javascript/issues/76) + +[子集 II-90](https://github.com/sl1673495/leetcode-javascript/issues/75) + +[ 组合总和 III-216](https://github.com/sl1673495/leetcode-javascript/issues/74) + +[组合总和 II-40](https://github.com/sl1673495/leetcode-javascript/issues/73) + +[组合总和-39](https://github.com/sl1673495/leetcode-javascript/issues/72) + +[子集-78](https://github.com/sl1673495/leetcode-javascript/issues/71) + +[组合-77](https://github.com/sl1673495/leetcode-javascript/issues/70) + +[全排列 II-47](https://github.com/sl1673495/leetcode-javascript/issues/69) + +[全排列-46](https://github.com/sl1673495/leetcode-javascript/issues/68) + +[分割回文串-131](https://github.com/sl1673495/leetcode-javascript/issues/67) -[盛水最多的容器-11](https://github.com/sl1673495/leetcode-javascript/issues/3) +[复原IP地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) + +[电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) + +[括号生成-22](https://github.com/sl1673495/leetcode-javascript/issues/31) ### 动态规划 +[最长的斐波那契子序列的长度-873](https://github.com/sl1673495/leetcode-javascript/issues/117) + +[最长重复子数组-718](https://github.com/sl1673495/leetcode-javascript/issues/114) + +[下降路径最小和-931](https://github.com/sl1673495/leetcode-javascript/issues/108) + +[最大正方形-221](https://github.com/sl1673495/leetcode-javascript/issues/101) + +[恢复空格-面试题 17.13](https://github.com/sl1673495/leetcode-javascript/issues/100) + +[最长单词-面试题 17.15](https://github.com/sl1673495/leetcode-javascript/issues/99) + [单词拆分 II-140](https://github.com/sl1673495/leetcode-javascript/issues/95) [单词拆分-139](https://github.com/sl1673495/leetcode-javascript/issues/93) @@ -87,95 +144,89 @@ [买卖股票的最佳时机-121](https://github.com/sl1673495/leetcode-javascript/issues/19) -[乘积最大子数组-152](https://github.com/sl1673495/leetcode-javascript/issues/18) - -[最大子序和-53](https://github.com/sl1673495/leetcode-javascript/issues/17) - -[分割等和子集(01 背包的变种)-416](https://github.com/sl1673495/leetcode-javascript/issues/16) - -[背包(01 背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) +### 双指针 -[使用最小花费爬楼梯-746](https://github.com/sl1673495/leetcode-javascript/issues/13) +[最接近的三数之和-16](https://github.com/sl1673495/leetcode-javascript/issues/115) -[零钱兑换 II-518](https://github.com/sl1673495/leetcode-javascript/issues/12) +[通过删除字母匹配到字典里最长单词-524](https://github.com/sl1673495/leetcode-javascript/issues/98) -[打家劫舍 - 198](https://github.com/sl1673495/leetcode-javascript/issues/10) +[搜索二维矩阵 II-240](https://github.com/sl1673495/leetcode-javascript/issues/96) -[完全平方数-279](https://github.com/sl1673495/leetcode-javascript/issues/9) +[判断子序列-392](https://github.com/sl1673495/leetcode-javascript/issues/89) -[整数拆分-343](https://github.com/sl1673495/leetcode-javascript/issues/7) +[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[斐波那契数-509](https://github.com/sl1673495/leetcode-javascript/issues/2) +[验证回文串-125](https://github.com/sl1673495/leetcode-javascript/issues/33) -### 递归与回溯 +[两数之和 II - 输入有序数组-167](https://github.com/sl1673495/leetcode-javascript/issues/32) -[单词搜索 II-212](https://github.com/sl1673495/leetcode-javascript/issues/92) +[合并两个有序数组-88](https://github.com/sl1673495/leetcode-javascript/issues/29) -[解数独-37](https://github.com/sl1673495/leetcode-javascript/issues/79) +[移动零-283](https://github.com/sl1673495/leetcode-javascript/issues/26) -[N 皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) +### 前缀和 -[单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) +[和为K的子数组-560](https://github.com/sl1673495/leetcode-javascript/issues/110) -[二进制手表-401](https://github.com/sl1673495/leetcode-javascript/issues/76) +### 位运算 -[子集 II-90](https://github.com/sl1673495/leetcode-javascript/issues/75) +[找不同-389](https://github.com/sl1673495/leetcode-javascript/issues/109) -[ 组合总和 III-216](https://github.com/sl1673495/leetcode-javascript/issues/74) +### 查找表 -[组合总和 II-40](https://github.com/sl1673495/leetcode-javascript/issues/73) +[找不同-389](https://github.com/sl1673495/leetcode-javascript/issues/109) -[组合总和-39](https://github.com/sl1673495/leetcode-javascript/issues/72) +[两个数组的交集 II-350](https://github.com/sl1673495/leetcode-javascript/issues/37) -[子集-78](https://github.com/sl1673495/leetcode-javascript/issues/71) +### BFS -[组合-77](https://github.com/sl1673495/leetcode-javascript/issues/70) +[跳跃游戏 IV-1345](https://github.com/sl1673495/leetcode-javascript/issues/103) -[全排列 II-47](https://github.com/sl1673495/leetcode-javascript/issues/69) +[跳跃游戏 III-1306](https://github.com/sl1673495/leetcode-javascript/issues/102) -[全排列-46](https://github.com/sl1673495/leetcode-javascript/issues/68) +[二叉树的最小深度-111](https://github.com/sl1673495/leetcode-javascript/issues/54) -[分割回文串-131](https://github.com/sl1673495/leetcode-javascript/issues/67) +[二叉树的最大深度-104](https://github.com/sl1673495/leetcode-javascript/issues/53) -[复原 IP 地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) +[二叉树的右视图-199](https://github.com/sl1673495/leetcode-javascript/issues/52) -[电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) +[二叉树的层序遍历-102](https://github.com/sl1673495/leetcode-javascript/issues/30) -### 贪心算法 +[相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) -[判断子序列-392](https://github.com/sl1673495/leetcode-javascript/issues/89) +### 排序 -[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) +[最长单词-面试题 17.15](https://github.com/sl1673495/leetcode-javascript/issues/99) -[买卖股票的最佳时机 II-122](https://github.com/sl1673495/leetcode-javascript/issues/20) +[通过删除字母匹配到字典里最长单词-524](https://github.com/sl1673495/leetcode-javascript/issues/98) -### 例题详解 +[快速排序](https://github.com/sl1673495/leetcode-javascript/issues/41) -[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) +[颜色分类-75](https://github.com/sl1673495/leetcode-javascript/issues/28) -[N 皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) +### 链表 -[单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) +[移除链表元素-203](https://github.com/sl1673495/leetcode-javascript/issues/97) -[二进制手表-401](https://github.com/sl1673495/leetcode-javascript/issues/76) +[两数相加-3](https://github.com/sl1673495/leetcode-javascript/issues/94) -[电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) +[两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) -[二叉树的所有路径-257](https://github.com/sl1673495/leetcode-javascript/issues/59) +[删除链表的倒数第N个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) -[路径总和-112](https://github.com/sl1673495/leetcode-javascript/issues/57) +[删除链表的节点-面试题18](https://github.com/sl1673495/leetcode-javascript/issues/40) -[两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) +[反转链表II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) -[无重复字符的最长子串-3](https://github.com/sl1673495/leetcode-javascript/issues/42) +[反转链表 206](https://github.com/sl1673495/leetcode-javascript/issues/38) -[二分查找-704](https://github.com/sl1673495/leetcode-javascript/issues/23) +### 贪心算法 -[背包(01 背包)](https://github.com/sl1673495/leetcode-javascript/issues/15) +[判断子序列-392](https://github.com/sl1673495/leetcode-javascript/issues/89) -[盛水最多的容器-11](https://github.com/sl1673495/leetcode-javascript/issues/3) +[分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[斐波那契数-509](https://github.com/sl1673495/leetcode-javascript/issues/2) +[买卖股票的最佳时机 II-122](https://github.com/sl1673495/leetcode-javascript/issues/20) ### DFS @@ -203,18 +254,12 @@ [二叉树的最大深度-104](https://github.com/sl1673495/leetcode-javascript/issues/53) -[二叉树的层序遍历](https://github.com/sl1673495/leetcode-javascript/issues/30) +[二叉树的层序遍历-102](https://github.com/sl1673495/leetcode-javascript/issues/30) [路径总和 II-113](https://github.com/sl1673495/leetcode-javascript/issues/27) [相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) -[打家劫舍 |||-337](https://github.com/sl1673495/leetcode-javascript/issues/11) - -[被围绕的区域-130](https://github.com/sl1673495/leetcode-javascript/issues/6) - -[岛屿的最大面积-695](https://github.com/sl1673495/leetcode-javascript/issues/5) - ### 二叉树 [二叉树的最近公共祖先-236](https://github.com/sl1673495/leetcode-javascript/issues/64) @@ -245,26 +290,12 @@ [二叉树的前序遍历-144](https://github.com/sl1673495/leetcode-javascript/issues/50) -[二叉树的层序遍历](https://github.com/sl1673495/leetcode-javascript/issues/30) +[二叉树的层序遍历-102](https://github.com/sl1673495/leetcode-javascript/issues/30) [路径总和 II-113](https://github.com/sl1673495/leetcode-javascript/issues/27) [相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) -### BFS - -[二叉树的最小深度-111](https://github.com/sl1673495/leetcode-javascript/issues/54) - -[二叉树的最大深度-104](https://github.com/sl1673495/leetcode-javascript/issues/53) - -[二叉树的右视图-199](https://github.com/sl1673495/leetcode-javascript/issues/52) - -[二叉树的层序遍历](https://github.com/sl1673495/leetcode-javascript/issues/30) - -[相同的树-100](https://github.com/sl1673495/leetcode-javascript/issues/21) - -[在每个树行中找最大值-515](https://github.com/sl1673495/leetcode-javascript/issues/4) - ### 栈和队列 [二叉树的右视图-199](https://github.com/sl1673495/leetcode-javascript/issues/52) @@ -289,22 +320,10 @@ [长度最小的子数组-209](https://github.com/sl1673495/leetcode-javascript/issues/36) -### 排序 - -[快速排序](https://github.com/sl1673495/leetcode-javascript/issues/41) - -[颜色分类-75](https://github.com/sl1673495/leetcode-javascript/issues/28) - -### 查找表 - -[两个数组的交集 II-350](https://github.com/sl1673495/leetcode-javascript/issues/37) - ### 数据结构 [LRU 缓存机制-146](https://github.com/sl1673495/leetcode-javascript/issues/35) -[实现 Trie (前缀树)-208](https://github.com/sl1673495/leetcode-javascript/issues/14) - ### 二分查找 [Pow(x, n)-50](https://github.com/sl1673495/leetcode-javascript/issues/25) From 123f28c842f9ea892a265b99db6fc3a05c193285 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 26 Sep 2021 16:52:34 +0800 Subject: [PATCH 142/145] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 78e30a6..f7924e9 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,11 @@ > 力扣的题解记录(JavaScript) +## 关于我 +大家好,我是 ssh,现在在字节跳动的 Web Infra 担任前端工程师。 + +我会在公众号「**[前端从进阶到入院](https://user-gold-cdn.xitu.io/2020/4/5/17149ccf687b7699?w=910&h=436&f=jpeg&s=78195)**」每日更新精心挑选的技术文章(标准就是我自己看了也会有收获),欢迎大家一起成长。 + ## 调试 提供了 .vscode 配置文件,在 vscode 中选择「小爬虫」图标,点击启动程序,即可启动断点调试。 From 1050a5b690d95b4fec4333a5e84c533538b65ef9 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sun, 26 Sep 2021 16:54:18 +0800 Subject: [PATCH 143/145] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f7924e9..0fee2c7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,9 @@ ## 关于我 大家好,我是 ssh,现在在字节跳动的 Web Infra 担任前端工程师。 -我会在公众号「**[前端从进阶到入院](https://user-gold-cdn.xitu.io/2020/4/5/17149ccf687b7699?w=910&h=436&f=jpeg&s=78195)**」每日更新精心挑选的技术文章(标准就是我自己看了也会有收获),欢迎大家一起成长。 +我会在公众号「前端从进阶到入院」每日更新精心挑选的技术文章(标准就是我自己看了也会有收获),欢迎大家一起成长。 + +![qrcode_for_gh_d2b31290dd8b_258](https://user-images.githubusercontent.com/23615778/134800856-9a44fa9a-4f1b-4884-a0b6-b58c5f3331df.jpg) ## 调试 From 904ac79bf001c9698dd63cefafa7fb8f0f1ca29d Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Sat, 9 Oct 2021 16:37:56 +0800 Subject: [PATCH 144/145] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0fee2c7..5e4db69 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ > 力扣的题解记录(JavaScript) ## 关于我 -大家好,我是 ssh,现在在字节跳动的 Web Infra 担任前端工程师。 +大家好,我是 ssh,现在在字节跳动的 Web Infra 担任前端工程师,微信:**[sshsunlight](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/017d568dc1d14cd883cc3238350a39ec~tplv-k3u1fbpfcp-watermark.image)**,欢迎找我交个朋友。 我会在公众号「前端从进阶到入院」每日更新精心挑选的技术文章(标准就是我自己看了也会有收获),欢迎大家一起成长。 From 402a8872548bda7c1cc7d56096889d526e51fcb3 Mon Sep 17 00:00:00 2001 From: ssh <949589855@qq.com> Date: Wed, 28 Jun 2023 21:17:25 +0800 Subject: [PATCH 145/145] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5e4db69..11f4963 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ## 关于我 大家好,我是 ssh,现在在字节跳动的 Web Infra 担任前端工程师,微信:**[sshsunlight](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/017d568dc1d14cd883cc3238350a39ec~tplv-k3u1fbpfcp-watermark.image)**,欢迎找我交个朋友。 -我会在公众号「前端从进阶到入院」每日更新精心挑选的技术文章(标准就是我自己看了也会有收获),欢迎大家一起成长。 +一些算法相关的资料,我放在「前端从进阶到入院」公众号里了,回复「资料」即可获取。 ![qrcode_for_gh_d2b31290dd8b_258](https://user-images.githubusercontent.com/23615778/134800856-9a44fa9a-4f1b-4884-a0b6-b58c5f3331df.jpg)