From d811a8ee78f6ec724f17e98780a0dccba85498ed Mon Sep 17 00:00:00 2001 From: wbingb Date: Thu, 11 Apr 2024 22:37:08 +0800 Subject: [PATCH 01/12] Summary: Added an iterative implementation of the find function in the union-find template to prevent stack overflow problems caused by recursive calls. --- .obsidian/app.json | 1 + .obsidian/appearance.json | 3 + .obsidian/core-plugins-migration.json | 30 ++++ .obsidian/core-plugins.json | 20 +++ .obsidian/workspace.json | 147 ++++++++++++++++++ .../1971.寻找图中是否存在路径.md | 24 ++- 6 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 .obsidian/app.json create mode 100644 .obsidian/appearance.json create mode 100644 .obsidian/core-plugins-migration.json create mode 100644 .obsidian/core-plugins.json create mode 100644 .obsidian/workspace.json diff --git a/.obsidian/app.json b/.obsidian/app.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/.obsidian/app.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json new file mode 100644 index 00000000..c8c365d8 --- /dev/null +++ b/.obsidian/appearance.json @@ -0,0 +1,3 @@ +{ + "accentColor": "" +} \ No newline at end of file diff --git a/.obsidian/core-plugins-migration.json b/.obsidian/core-plugins-migration.json new file mode 100644 index 00000000..436f43cf --- /dev/null +++ b/.obsidian/core-plugins-migration.json @@ -0,0 +1,30 @@ +{ + "file-explorer": true, + "global-search": true, + "switcher": true, + "graph": true, + "backlink": true, + "canvas": true, + "outgoing-link": true, + "tag-pane": true, + "properties": false, + "page-preview": true, + "daily-notes": true, + "templates": true, + "note-composer": true, + "command-palette": true, + "slash-command": false, + "editor-status": true, + "bookmarks": true, + "markdown-importer": false, + "zk-prefixer": false, + "random-note": false, + "outline": true, + "word-count": true, + "slides": false, + "audio-recorder": false, + "workspaces": false, + "file-recovery": true, + "publish": false, + "sync": false +} \ No newline at end of file diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json new file mode 100644 index 00000000..9405bfdc --- /dev/null +++ b/.obsidian/core-plugins.json @@ -0,0 +1,20 @@ +[ + "file-explorer", + "global-search", + "switcher", + "graph", + "backlink", + "canvas", + "outgoing-link", + "tag-pane", + "page-preview", + "daily-notes", + "templates", + "note-composer", + "command-palette", + "editor-status", + "bookmarks", + "outline", + "word-count", + "file-recovery" +] \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json new file mode 100644 index 00000000..144fe03f --- /dev/null +++ b/.obsidian/workspace.json @@ -0,0 +1,147 @@ +{ + "main": { + "id": "14608cf8b641f651", + "type": "split", + "children": [ + { + "id": "7b559c19d2418ebd", + "type": "tabs", + "children": [ + { + "id": "a006865600bc4e20", + "type": "leaf", + "state": { + "type": "empty", + "state": {} + } + } + ] + } + ], + "direction": "vertical" + }, + "left": { + "id": "340e6a79c670a47b", + "type": "split", + "children": [ + { + "id": "c5f81ca398c18cda", + "type": "tabs", + "children": [ + { + "id": "51f9f8f44ecceb89", + "type": "leaf", + "state": { + "type": "file-explorer", + "state": { + "sortOrder": "alphabetical" + } + } + }, + { + "id": "0cff567244d7cff5", + "type": "leaf", + "state": { + "type": "search", + "state": { + "query": "1971", + "matchingCase": false, + "explainSearch": false, + "collapseAll": false, + "extraContext": false, + "sortOrder": "alphabetical" + } + } + }, + { + "id": "209574c920774a4d", + "type": "leaf", + "state": { + "type": "bookmarks", + "state": {} + } + } + ], + "currentTab": 1 + } + ], + "direction": "horizontal", + "width": 300 + }, + "right": { + "id": "93ce8f4c11893983", + "type": "split", + "children": [ + { + "id": "da8cbb56e5ee7c52", + "type": "tabs", + "children": [ + { + "id": "dfccbc1ebc0bb831", + "type": "leaf", + "state": { + "type": "backlink", + "state": { + "collapseAll": false, + "extraContext": false, + "sortOrder": "alphabetical", + "showSearch": false, + "searchQuery": "", + "backlinkCollapsed": false, + "unlinkedCollapsed": true + } + } + }, + { + "id": "9d2201419a111fe4", + "type": "leaf", + "state": { + "type": "outgoing-link", + "state": { + "linksCollapsed": false, + "unlinkedCollapsed": true + } + } + }, + { + "id": "dc86ccf1b01bc02c", + "type": "leaf", + "state": { + "type": "tag", + "state": { + "sortOrder": "frequency", + "useHierarchy": true + } + } + }, + { + "id": "ec512545d2a7f2e5", + "type": "leaf", + "state": { + "type": "outline", + "state": {} + } + } + ] + } + ], + "direction": "horizontal", + "width": 300, + "collapsed": true + }, + "left-ribbon": { + "hiddenItems": { + "switcher:打开快速切换": false, + "graph:查看关系图谱": false, + "canvas:新建白板": false, + "daily-notes:打开/创建今天的日记": false, + "templates:插入模板": false, + "command-palette:打开命令面板": false + } + }, + "active": "a006865600bc4e20", + "lastOpenFiles": [ + "problems/1971.寻找图中是否存在路径.md", + "README.md" + ] +} \ No newline at end of file diff --git a/problems/1971.寻找图中是否存在路径.md b/problems/1971.寻找图中是否存在路径.md index 89a2a1fe..4123c03e 100644 --- a/problems/1971.寻找图中是否存在路径.md +++ b/problems/1971.寻找图中是否存在路径.md @@ -47,11 +47,22 @@ void init() { father[i] = i; } } -// 并查集里寻根的过程 +// 并查集里寻根的过程,这里递归调用当题目数据过多,递归调用可能会发生栈溢出 + int find(int u) { return u == father[u] ? u : father[u] = find(father[u]); // 路径压缩 } +// 使用迭代的方法可以避免栈溢出问题 +int find(int x) { + while (x != parent[x]) { + // 路径压缩,直接将x链接到其祖先节点,减少树的高度 + parent[x] = parent[parent[x]]; + x = parent[x]; + } +return x; +} + // 判断 u 和 v是否找到同一个根 bool isSame(int u, int v) { u = find(u); @@ -84,6 +95,8 @@ void join(int u, int v) { 此时我们就可以直接套用并查集模板。 +本题在join函数调用find函数时如果是递归调用会发生栈溢出提示,建议使用迭代方法 + 使用 join(int u, int v)将每条边加入到并查集。 最后 isSame(int u, int v) 判断是否是同一个根 就可以了。 @@ -102,8 +115,13 @@ private: } } // 并查集里寻根的过程 - int find(int u) { - return u == father[u] ? u : father[u] = find(father[u]); + int find(int x) { + while (x != parent[x]) { + // 路径压缩,直接将x链接到其祖先节点,减少树的高度 + parent[x] = parent[parent[x]]; + x = parent[x]; + } + return x; } // 判断 u 和 v是否找到同一个根 From 8150d654ba7291b60b8d3a344abc015cd5f8c8c3 Mon Sep 17 00:00:00 2001 From: Yunliuyu <96341845+YShelter@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:47:33 +0800 Subject: [PATCH 02/12] =?UTF-8?q?Update=200142.=E7=8E=AF=E5=BD=A2=E9=93=BE?= =?UTF-8?q?=E8=A1=A8II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 错别字,再看本篇内容 --- problems/0142.环形链表II.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index d97b160b..7cda58c3 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -26,7 +26,7 @@ ## 算法公开课 -**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。** +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频再看本篇题解,更有助于大家对链表的理解。** ## 思路 From a47fcbb5b6f0cf3c79b6b693a39b157fbfb878f6 Mon Sep 17 00:00:00 2001 From: songlongfei1 Date: Tue, 30 Apr 2024 18:16:57 +0800 Subject: [PATCH 03/12] =?UTF-8?q?docs:=20=E3=80=900055=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E3=80=91JavaScript=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0055.右旋字符串.md | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/problems/kamacoder/0055.右旋字符串.md b/problems/kamacoder/0055.右旋字符串.md index 71371860..a9f57192 100644 --- a/problems/kamacoder/0055.右旋字符串.md +++ b/problems/kamacoder/0055.右旋字符串.md @@ -254,7 +254,37 @@ func main(){ ### JavaScript: +```javascript +// JS中字符串内不可单独修改 +// 右旋转 +function reverseLeftWords(s, k) { + const reverse = (sList, start, end) => { + for (let i = start, j = end; i < j; i++, j--) { + [sList[i], sList[j]] = [sList[j], sList[i]]; + } + } + const sList = Array.from(s); + reverse(sList, 0, sList.length - k - 1); + reverse(sList, sList.length - k, sList.length - 1); + reverse(sList, 0, sList.length - 1); + return sList.join(''); +} + +// 左旋转 +var reverseLeftWords = function(s, n) { + const reverse = (sList, start, end) => { + for (let i = start, j = end; i < j; i++, j--) { + [sList[i], sList[j]] = [sList[j], sList[i]]; + } + } + const sList = s.split(''); + reverse(sList, 0, n - 1); + reverse(sList, n, sList.length - 1); + reverse(sList, 0, sList.length - 1); + return sList.join(''); +}; +``` ### TypeScript: From d488652a135250c82fb9c8e984922ba834665e14 Mon Sep 17 00:00:00 2001 From: Yunliuyu <96341845+YShelter@users.noreply.github.com> Date: Thu, 2 May 2024 21:45:01 +0800 Subject: [PATCH 04/12] =?UTF-8?q?Update=200454.=E5=9B=9B=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=8A=A0II.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 又是错别字,在 -> 再 碎碎念:这样子可以吗? --- problems/0454.四数相加II.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index db9a9e43..83dea97e 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -54,7 +54,7 @@ 1. 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。 2. 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。 3. 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。 -4. 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。 +4. 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。 5. 最后返回统计值 count 就可以了 C++代码: @@ -71,7 +71,7 @@ public: } } int count = 0; // 统计a+b+c+d = 0 出现的次数 - // 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 + // 再遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。 for (int c : C) { for (int d : D) { if (umap.find(0 - (c + d)) != umap.end()) { From fffec6ec918a3d45950497e14778882b258c73ff Mon Sep 17 00:00:00 2001 From: heystone999 <52831724+heystone999@users.noreply.github.com> Date: Fri, 3 May 2024 17:50:11 -0400 Subject: [PATCH 05/12] =?UTF-8?q?style:=20=E4=BB=A3=E7=A0=81highlight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1020.飞地的数量.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/1020.飞地的数量.md b/problems/1020.飞地的数量.md index 3488777a..5063139f 100644 --- a/problems/1020.飞地的数量.md +++ b/problems/1020.飞地的数量.md @@ -395,7 +395,7 @@ class Solution { 深度优先遍历 -```Python3 +```Python class Solution: def __init__(self): self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 @@ -442,7 +442,7 @@ class Solution: 广度优先遍历 -```Python3 +```Python class Solution: def __init__(self): self.position = [[-1, 0], [0, 1], [1, 0], [0, -1]] # 四个方向 From cd03a5b79a5deb90e444d46ba4c22cffd9478440 Mon Sep 17 00:00:00 2001 From: markwang Date: Tue, 7 May 2024 10:57:05 +0800 Subject: [PATCH 06/12] =?UTF-8?q?15.=E4=B8=89=E6=95=B0=E4=B9=8B=E5=92=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Go=E5=93=88=E5=B8=8C=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0015.三数之和.md | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index ae218385..f7146907 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -403,6 +403,7 @@ class Solution: ``` ### Go: +(版本一) 双指针 ```Go func threeSum(nums []int) [][]int { @@ -442,6 +443,42 @@ func threeSum(nums []int) [][]int { return res } ``` +(版本二) 哈希解法 + +```Go +func threeSum(nums []int) [][]int { + res := make([][]int, 0) + sort.Ints(nums) + // 找出a + b + c = 0 + // a = nums[i], b = nums[j], c = -(a + b) + for i := 0; i < len(nums); i++ { + // 排序之后如果第一个元素已经大于零,那么不可能凑成三元组 + if nums[i] > 0 { + break + } + // 三元组元素a去重 + if i > 0 && nums[i] == nums[i-1] { + continue + } + set := make(map[int]struct{}) + for j := i + 1; j < len(nums); j++ { + // 三元组元素b去重 + if j > i + 2 && nums[j] == nums[j-1] && nums[j-1] == nums[j-2] { + continue + } + c := -nums[i] - nums[j] + if _, ok := set[c]; ok { + res = append(res, []int{nums[i], nums[j], c}) + // 三元组元素c去重 + delete(set, c) + } else { + set[nums[j]] = struct{}{} + } + } + } + return res +} +``` ### JavaScript: From fa6c8d2e98240323085e9970c5016a951f916eff Mon Sep 17 00:00:00 2001 From: ray <109325327+raydemo1@users.noreply.github.com> Date: Wed, 8 May 2024 18:45:21 +0800 Subject: [PATCH 07/12] =?UTF-8?q?Update=200102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86.md=20Python?= =?UTF-8?q?=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E9=80=92=E5=BD=92=E6=B3=95?= =?UTF-8?q?=E6=9B=B4=E7=AE=80=E6=B4=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 28 ++++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index ab6b07bf..cdc7a783 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -201,7 +201,7 @@ class Solution: return result ``` ```python -# 递归法 +#递归法 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): @@ -210,18 +210,24 @@ class Solution: # self.right = right class Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + if not root: + return [] + levels = [] - self.helper(root, 0, levels) + + def traverse(node, level): + if not node: + return + + if len(levels) == level: + levels.append([]) + + levels[level].append(node.val) + traverse(node.left, level + 1) + traverse(node.right, level + 1) + + traverse(root, 0) return levels - - def helper(self, node, level, levels): - if not node: - return - if len(levels) == level: - levels.append([]) - levels[level].append(node.val) - self.helper(node.left, level + 1, levels) - self.helper(node.right, level + 1, levels) ``` From 60ec1b83e2d697fdf5f31118509f362e42d50664 Mon Sep 17 00:00:00 2001 From: coffeeboy <114488340+zp-coffee@users.noreply.github.com> Date: Fri, 10 May 2024 10:20:57 +0800 Subject: [PATCH 08/12] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新排版以及修改一个错别字 --- problems/二叉树的迭代遍历.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index 8549bac1..5f59c388 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -117,7 +117,7 @@ public: ### 后序遍历(迭代法) -再来看后序遍历,先序遍历是中左右,后续遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: +再来看后序遍历,先序遍历是中左右,后序遍历是左右中,那么我们只需要调整一下先序遍历的代码顺序,就变成中右左的遍历顺序,然后在反转result数组,输出的结果顺序就是左右中了,如下图: ![前序到后序](https://code-thinking-1253855093.file.myqcloud.com/pics/20200808200338924.png) @@ -153,7 +153,7 @@ public: 上面这句话,可能一些同学不太理解,建议自己亲手用迭代法,先写出来前序,再试试能不能写出中序,就能理解了。 -**那么问题又来了,难道 二叉树前后中序遍历的迭代法实现,就不能风格统一么(即前序遍历 改变代码顺序就可以实现中序 和 后序)?** +**那么问题又来了,难道二叉树前后中序遍历的迭代法实现,就不能风格统一么(即前序遍历改变代码顺序就可以实现中序 和 后序)?** 当然可以,这种写法,还不是很好理解,我们将在下一篇文章里重点讲解,敬请期待! From 12fffbeaa86bfc293caf22b4b5fc05d4e929d738 Mon Sep 17 00:00:00 2001 From: wbingb Date: Wed, 15 May 2024 10:39:13 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .obsidian/app.json | 1 - .obsidian/appearance.json | 3 - .obsidian/core-plugins-migration.json | 30 ------ .obsidian/core-plugins.json | 20 ---- .obsidian/workspace.json | 147 -------------------------- 5 files changed, 201 deletions(-) delete mode 100644 .obsidian/app.json delete mode 100644 .obsidian/appearance.json delete mode 100644 .obsidian/core-plugins-migration.json delete mode 100644 .obsidian/core-plugins.json delete mode 100644 .obsidian/workspace.json diff --git a/.obsidian/app.json b/.obsidian/app.json deleted file mode 100644 index 9e26dfee..00000000 --- a/.obsidian/app.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/.obsidian/appearance.json b/.obsidian/appearance.json deleted file mode 100644 index c8c365d8..00000000 --- a/.obsidian/appearance.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "accentColor": "" -} \ No newline at end of file diff --git a/.obsidian/core-plugins-migration.json b/.obsidian/core-plugins-migration.json deleted file mode 100644 index 436f43cf..00000000 --- a/.obsidian/core-plugins-migration.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "file-explorer": true, - "global-search": true, - "switcher": true, - "graph": true, - "backlink": true, - "canvas": true, - "outgoing-link": true, - "tag-pane": true, - "properties": false, - "page-preview": true, - "daily-notes": true, - "templates": true, - "note-composer": true, - "command-palette": true, - "slash-command": false, - "editor-status": true, - "bookmarks": true, - "markdown-importer": false, - "zk-prefixer": false, - "random-note": false, - "outline": true, - "word-count": true, - "slides": false, - "audio-recorder": false, - "workspaces": false, - "file-recovery": true, - "publish": false, - "sync": false -} \ No newline at end of file diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json deleted file mode 100644 index 9405bfdc..00000000 --- a/.obsidian/core-plugins.json +++ /dev/null @@ -1,20 +0,0 @@ -[ - "file-explorer", - "global-search", - "switcher", - "graph", - "backlink", - "canvas", - "outgoing-link", - "tag-pane", - "page-preview", - "daily-notes", - "templates", - "note-composer", - "command-palette", - "editor-status", - "bookmarks", - "outline", - "word-count", - "file-recovery" -] \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json deleted file mode 100644 index 144fe03f..00000000 --- a/.obsidian/workspace.json +++ /dev/null @@ -1,147 +0,0 @@ -{ - "main": { - "id": "14608cf8b641f651", - "type": "split", - "children": [ - { - "id": "7b559c19d2418ebd", - "type": "tabs", - "children": [ - { - "id": "a006865600bc4e20", - "type": "leaf", - "state": { - "type": "empty", - "state": {} - } - } - ] - } - ], - "direction": "vertical" - }, - "left": { - "id": "340e6a79c670a47b", - "type": "split", - "children": [ - { - "id": "c5f81ca398c18cda", - "type": "tabs", - "children": [ - { - "id": "51f9f8f44ecceb89", - "type": "leaf", - "state": { - "type": "file-explorer", - "state": { - "sortOrder": "alphabetical" - } - } - }, - { - "id": "0cff567244d7cff5", - "type": "leaf", - "state": { - "type": "search", - "state": { - "query": "1971", - "matchingCase": false, - "explainSearch": false, - "collapseAll": false, - "extraContext": false, - "sortOrder": "alphabetical" - } - } - }, - { - "id": "209574c920774a4d", - "type": "leaf", - "state": { - "type": "bookmarks", - "state": {} - } - } - ], - "currentTab": 1 - } - ], - "direction": "horizontal", - "width": 300 - }, - "right": { - "id": "93ce8f4c11893983", - "type": "split", - "children": [ - { - "id": "da8cbb56e5ee7c52", - "type": "tabs", - "children": [ - { - "id": "dfccbc1ebc0bb831", - "type": "leaf", - "state": { - "type": "backlink", - "state": { - "collapseAll": false, - "extraContext": false, - "sortOrder": "alphabetical", - "showSearch": false, - "searchQuery": "", - "backlinkCollapsed": false, - "unlinkedCollapsed": true - } - } - }, - { - "id": "9d2201419a111fe4", - "type": "leaf", - "state": { - "type": "outgoing-link", - "state": { - "linksCollapsed": false, - "unlinkedCollapsed": true - } - } - }, - { - "id": "dc86ccf1b01bc02c", - "type": "leaf", - "state": { - "type": "tag", - "state": { - "sortOrder": "frequency", - "useHierarchy": true - } - } - }, - { - "id": "ec512545d2a7f2e5", - "type": "leaf", - "state": { - "type": "outline", - "state": {} - } - } - ] - } - ], - "direction": "horizontal", - "width": 300, - "collapsed": true - }, - "left-ribbon": { - "hiddenItems": { - "switcher:打开快速切换": false, - "graph:查看关系图谱": false, - "canvas:新建白板": false, - "daily-notes:打开/创建今天的日记": false, - "templates:插入模板": false, - "command-palette:打开命令面板": false - } - }, - "active": "a006865600bc4e20", - "lastOpenFiles": [ - "problems/1971.寻找图中是否存在路径.md", - "README.md" - ] -} \ No newline at end of file From 96359371d8668d1648cce37b2cfe3614fd01a49b Mon Sep 17 00:00:00 2001 From: markwang Date: Wed, 15 May 2024 10:52:59 +0800 Subject: [PATCH 10/12] =?UTF-8?q?459.=E9=87=8D=E5=A4=8D=E7=9A=84=E5=AD=90?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=A2=9E=E5=8A=A0Go=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=8C=B9=E9=85=8D=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index 1028bf1e..51425796 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -403,6 +403,18 @@ func repeatedSubstringPattern(s string) bool { } ``` +移动匹配 + +```go +func repeatedSubstringPattern(s string) bool { + if len(s) == 0 { + return false + } + t := s + s + return strings.Contains(t[1:len(t)-1], s) +} +``` + ### JavaScript: > 前缀表统一减一 From f5a5d2005e394f0d5de2adf9d88f839796f3267b Mon Sep 17 00:00:00 2001 From: GP Date: Sat, 18 May 2024 13:05:11 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E7=89=88=E6=9B=B4=E9=80=9A=E4=BF=97=E6=98=93=E6=87=82=E7=9A=84?= =?UTF-8?q?Java=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0054.螺旋矩阵.md | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index 4d54ccd6..d7cfc7bf 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -200,6 +200,79 @@ class Solution { } ``` +```java +class Solution { + public List spiralOrder(int[][] matrix) { + List res = new ArrayList<>(); // 存放结果 + if (matrix.length == 0 || matrix[0].length == 0) + return res; + int rows = matrix.length, columns = matrix[0].length; + int startx = 0, starty = 0; // 定义每循环一个圈的起始位置 + int loop = 0; // 循环次数 + int offset = 1; // 每一圈循环,需要控制每一条边遍历的长度 + while (loop < Math.min(rows, columns) / 2) { + int i = startx; + int j = starty; + // 模拟填充上行从左到右(左闭右开) + for (; j < columns - offset; j++) { + res.add(matrix[i][j]); + } + // 模拟填充右列从上到下(左闭右开) + for (; i < rows - offset; i++) { + res.add(matrix[i][j]); + } + // 模拟填充下行从右到左(左闭右开) + for (; j > starty; j--) { + res.add(matrix[i][j]); + } + // 模拟填充左列从下到上(左闭右开) + for (; i > startx; i--) { + res.add(matrix[i][j]); + } + + // 起始位置加1 循环次数加1 并控制每条边遍历的长度 + startx++; + starty++; + offset++; + loop++; + } + + // 如果列或行中的最小值为奇数 则一定有未遍历的部分 + // 可以自行画图理解 + if (Math.min(rows, columns) % 2 == 1) { + // 当行大于列时 未遍历的部分是列 + // (startx, starty)即下一个要遍历位置 从该位置出发 遍历完未遍历的列 + // 遍历次数为rows - columns + 1 + if (rows > columns) { + for (int i = 0; i < rows - columns + 1; i++) { + res.add(matrix[startx++][starty]); + } + } else { + // 此处与上面同理 遍历完未遍历的行 + for (int i = 0; i < columns - rows + 1; i++) { + res.add(matrix[startx][starty++]); + } + } + } + + return res; + } +} +``` + + + + + + + + + + + + + + ### Javascript ``` /** From 00c9165e3ca52cebaa3449cc996a43fdd4184c93 Mon Sep 17 00:00:00 2001 From: GPFE05 <113404586+GPFE05@users.noreply.github.com> Date: Sun, 19 May 2024 11:06:26 +0800 Subject: [PATCH 12/12] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0054.螺旋矩阵.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index d7cfc7bf..96d17413 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -260,19 +260,6 @@ class Solution { } ``` - - - - - - - - - - - - - ### Javascript ``` /**