From 1e207bfc6b2f546c8a38ed005416076d0da9ef46 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 24 Aug 2023 20:47:01 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A00077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 8d448739..a7f00ffe 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -764,6 +764,35 @@ object Solution { } ``` +### Ruby + +```ruby + +def combine(n, k) + result = [] + path = [] + backtracking(result, path, n, 1, k) + return result +end + +#剪枝优化 +def backtracking(result, path, n, j, k) + if path.size == k + result << path.map {|item| item} + return + end + + for i in j..(n-(k - path.size)) + 1 + #处理节点 + path << i + backtracking(result, path, n, i + 1, k) + #回溯,撤销处理过的节点 + path.pop + end +end + +``` +

From 2393f86448a3ae64514bfc4dea3ab0bb2d5fc8f0 Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 11:17:01 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E6=94=B9=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer58-II.左旋转字符串.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index a3fb7ab3..f58135eb 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -120,8 +120,9 @@ class Solution { ``` ```java -//解法二:空间复杂度:O(1)。用原始数组来进行反转操作 -//思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个 +// 解法二 +// 空间复杂度:O(n)。String 的 toCharArray() 方法底层会 new 一个和原字符串相同大小的 char 数组 +// 思路为:先整个字符串反转,再反转前面的,最后反转后面 n 个 class Solution { public String reverseLeftWords(String s, int n) { char[] chars = s.toCharArray(); @@ -418,3 +419,4 @@ impl Solution { + From a03b6a37de06660adabcb44fc32fdaed2fc54046 Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 11:52:13 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer58-II.左旋转字符串.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index f58135eb..310f2092 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -418,5 +418,4 @@ impl Solution {

- - + \ No newline at end of file From a2f4933faa0dcaf2d4de49ab3d951f1e20261ca0 Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 11:54:02 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/剑指Offer58-II.左旋转字符串.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/剑指Offer58-II.左旋转字符串.md b/problems/剑指Offer58-II.左旋转字符串.md index 310f2092..a516213c 100644 --- a/problems/剑指Offer58-II.左旋转字符串.md +++ b/problems/剑指Offer58-II.左旋转字符串.md @@ -418,4 +418,4 @@ impl Solution {

- \ No newline at end of file + From fd5a171e886df7c9cd8090272f272b4e2a16fc5d Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 20:15:48 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=A4=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index 0c1a526f..bbd793d1 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -311,7 +311,7 @@ class Solution { ``` ```java -//解法三:双反转+移位,在原始数组上进行反转。空间复杂度O(1) +//解法三:双反转+移位,String 的 toCharArray() 方法底层会 new 一个和原字符串相同大小的 char 数组,空间复杂度:O(n) class Solution { /** * 思路: @@ -976,3 +976,4 @@ char * reverseWords(char * s){ + From 43e0fd9d7712453fd884d4a319c89e7f8edd0c9b Mon Sep 17 00:00:00 2001 From: hbm666 <1208415748@qq.com> Date: Sun, 27 Aug 2023 20:23:05 +0800 Subject: [PATCH 06/12] =?UTF-8?q?151.=E7=BF=BB=E8=BD=AC=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=20-=20Java?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=B3=A8=E9=87=8A=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index bbd793d1..111c07e4 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -976,4 +976,3 @@ char * reverseWords(char * s){ - From ca197783d20194a10202d9033e5b1ec6da3c9d2a Mon Sep 17 00:00:00 2001 From: Siqi Cao <90129698+gitcsq@users.noreply.github.com> Date: Sun, 27 Aug 2023 14:13:18 -0400 Subject: [PATCH 07/12] =?UTF-8?q?Update=20=E4=BF=AE=E6=AD=A3=200225.?= =?UTF-8?q?=E7=94=A8=E9=98=9F=E5=88=97=E5=AE=9E=E7=8E=B0=E6=A0=88=20python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0225.用队列实现栈.md | 37 ++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md index 13b742f8..14b7beb9 100644 --- a/problems/0225.用队列实现栈.md +++ b/problems/0225.用队列实现栈.md @@ -454,13 +454,34 @@ class MyStack: def top(self) -> int: """ + 写法一: 1. 首先确认不空 - 2. 我们仅有in会存放数据,所以返回第一个即可 + 2. 我们仅有in会存放数据,所以返回第一个即可(这里实际上用到了栈) + 写法二: + 1. 首先确认不空 + 2. 因为队列的特殊性,FIFO,所以我们只有在pop()的时候才会使用queue_out + 3. 先把queue_in中的所有元素(除了最后一个),依次出列放进queue_out + 4. 交换in和out,此时out里只有一个元素 + 5. 把out中的pop出来,即是原队列的最后一个,并使用temp变量暂存 + 6. 把temp追加到queue_in的末尾 """ + # 写法一: + # if self.empty(): + # return None + + # return self.queue_in[-1] # 这里实际上用到了栈,因为直接获取了queue_in的末尾元素 + + # 写法二: if self.empty(): return None + + for i in range(len(self.queue_in) - 1): + self.queue_out.append(self.queue_in.popleft()) - return self.queue_in[-1] + self.queue_in, self.queue_out = self.queue_out, self.queue_in + temp = self.queue_out.popleft() + self.queue_in.append(temp) + return temp def empty(self) -> bool: @@ -488,9 +509,19 @@ class MyStack: return self.que.popleft() def top(self) -> int: + # 写法一: + # if self.empty(): + # return None + # return self.que[-1] + + # 写法二: if self.empty(): return None - return self.que[-1] + for i in range(len(self.que)-1): + self.que.append(self.que.popleft()) + temp = self.que.popleft() + self.que.append(temp) + return temp def empty(self) -> bool: return not self.que From 076d87605e29d2474f89d2e7312d2a9a2fc69818 Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:26:53 +0800 Subject: [PATCH 08/12] =?UTF-8?q?Update=200797.=E6=89=80=E6=9C=89=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E7=9A=84=E8=B7=AF=E5=BE=84=20Go=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0797.所有可能的路径.md | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index ec8288c6..5c209f60 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -217,6 +217,34 @@ class Solution: self.path.pop() # 回溯 ``` +### Go + +```go +func allPathsSourceTarget(graph [][]int) [][]int { + result := make([][]int, 0) + + var trace func(path []int, step int) + trace = func(path []int, step int){ + // 从0遍历到length-1 + if step == len(graph) - 1{ + tmp := make([]int, len(path)) + copy(tmp, path) + result = append(result, tmp) + return + } + + for i := 0; i < len(graph[step]); i++{ + next := append(path, graph[step][i]) + trace(next, graph[step][i]) + } + } + // 从0开始,开始push 0进去 + trace([]int{0}, 0) + return result +} + +``` +

From 0bfd53394e9062d284586319a50aaf6cc23755eb Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:35:06 +0800 Subject: [PATCH 09/12] =?UTF-8?q?Update=200797.=E6=89=80=E6=9C=89=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E7=9A=84=E8=B7=AF=E5=BE=84=20Go=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新函数名为dfs --- problems/0797.所有可能的路径.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index 5c209f60..e97a6d0d 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -223,8 +223,8 @@ class Solution: func allPathsSourceTarget(graph [][]int) [][]int { result := make([][]int, 0) - var trace func(path []int, step int) - trace = func(path []int, step int){ + var dfs func(path []int, step int) + dfs = func(path []int, step int){ // 从0遍历到length-1 if step == len(graph) - 1{ tmp := make([]int, len(path)) @@ -235,11 +235,11 @@ func allPathsSourceTarget(graph [][]int) [][]int { for i := 0; i < len(graph[step]); i++{ next := append(path, graph[step][i]) - trace(next, graph[step][i]) + dfs(next, graph[step][i]) } } // 从0开始,开始push 0进去 - trace([]int{0}, 0) + dfs([]int{0}, 0) return result } From 6dcc537cef392a24caa636667f1bd4730d9f2898 Mon Sep 17 00:00:00 2001 From: Shixiaocaia <68102662+shixiaocaia@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:56:10 +0800 Subject: [PATCH 10/12] =?UTF-8?q?Update=200200.=E5=B2=9B=E5=B1=BF=E6=95=B0?= =?UTF-8?q?=E9=87=8F.=E6=B7=B1=E6=90=9C=E7=89=88=20Go=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0200.岛屿数量.深搜版.md | 52 ++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/problems/0200.岛屿数量.深搜版.md b/problems/0200.岛屿数量.深搜版.md index f610e323..ad4a6ede 100644 --- a/problems/0200.岛屿数量.深搜版.md +++ b/problems/0200.岛屿数量.深搜版.md @@ -219,7 +219,7 @@ class Solution { } ``` -Python: +### Python: ```python # 版本一 @@ -279,6 +279,56 @@ class Solution: return result ``` +### Go + +```go +func numIslands(grid [][]byte) int { + // 用1标记已访问 + visited := make([][]int, len(grid)) + for i := 0; i < len(visited); i++{ + visited[i] = make([]int, len(grid[0])) + } + + var bfs func(x, y int) + bfs = func(x, y int){ + stack := make([][]int, 0) + stack = append(stack, []int{x, y}) + moveX := []int{1, -1, 0, 0} + moveY := []int{0, 0, 1, -1} + + for len(stack) != 0{ + node := stack[len(stack) - 1] + stack = stack[:len(stack) - 1] + + for i := 0; i < 4; i++{ + dx := moveX[i] + node[0] + dy := moveY[i] + node[1] + if dx < 0 || dx >= len(grid) || dy < 0 || dy >= len(grid[0]) || visited[dx][dy] == 1{ + continue + } + visited[dx][dy] = 1 + if grid[dx][dy] == '1'{ + stack = append(stack, []int{dx,dy}) + } + } + } + } + + result := 0 + for i := 0; i < len(grid); i++{ + for j := 0; j < len(grid[0]); j++{ + if visited[i][j] == 0 && grid[i][j] == '1'{ + bfs(i, j) + visited[i][j] = 1 + result++ + } + } + } + + return result +} +``` +

From 6141fc92bb04dc9f09402b77341562efb8b952d7 Mon Sep 17 00:00:00 2001 From: han Date: Wed, 30 Aug 2023 16:28:31 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A00017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?=20Ruby=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index b3ba1e5e..90296efd 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -694,6 +694,44 @@ object Solution { } ``` +### Ruby +```ruby +def letter_combinations(digits) + letter_map = { + 2 => ['a','b','c'], + 3 => ['d','e','f'], + 4 => ['g','h','i'], + 5 => ['j','k','l'], + 6 => ['m','n','o'], + 7 => ['p','q','r','s'], + 8 => ['t','u','v'], + 9 => ['w','x','y','z'] + } + + result = [] + path = [] + + return result if digits.size == 0 + + backtracking(result, letter_map, digits.split(''), path, 0) + result +end + +def backtracking(result, letter_map, digits, path, index) + if path.size == digits.size + result << path.join('') + return + end + + hash[digits[index].to_i].each do |chr| + path << chr + #index + 1代表处理下一个数字 + backtracking(result, letter_map, digits, path, index + 1) + #回溯,撤销处理过的数字 + path.pop + end +end +```

From 9d32bab764755465017422aadab557c612e604e3 Mon Sep 17 00:00:00 2001 From: jinjjm <1185838762@qq.com> Date: Thu, 31 Aug 2023 22:14:16 +0800 Subject: [PATCH 12/12] =?UTF-8?q?js=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0200.岛屿数量.广搜版.md | 36 +++++++++++++++++++++++ problems/0200.岛屿数量.深搜版.md | 33 +++++++++++++++++++++ problems/0695.岛屿的最大面积.md | 38 +++++++++++++++++++++++++ problems/0797.所有可能的路径.md | 22 ++++++++++++++ 4 files changed, 129 insertions(+) diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md index 5b9d90aa..5acdf6e2 100644 --- a/problems/0200.岛屿数量.广搜版.md +++ b/problems/0200.岛屿数量.广搜版.md @@ -239,6 +239,42 @@ class Solution: visited[next_i][next_j] = True ``` +### JavaScript +```javascript +var numIslands = function (grid) { + let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + let bfs = (grid, visited, x, y) => { + let queue = []; + queue.push([x, y]); + visited[x][y] = true; + while (queue.length) { + let top = queue.shift();//取出队列头部元素 + console.log(top) + for (let i = 0; i < 4; i++) { + let nextX = top[0] + dir[i][0] + let nextY = top[1] + dir[i][1] + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) + continue; + if (!visited[nextX][nextY] && grid[nextX][nextY] === "1") { + queue.push([nextX, nextY]) + visited[nextX][nextY] = true + } + } + } + } + let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false)) + let res = 0 + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (!visited[i][j] && grid[i][j] === "1") { + ++res; + bfs(grid, visited, i, j); + } + } + } + return res +}; +```

diff --git a/problems/0200.岛屿数量.深搜版.md b/problems/0200.岛屿数量.深搜版.md index f610e323..b8f2c992 100644 --- a/problems/0200.岛屿数量.深搜版.md +++ b/problems/0200.岛屿数量.深搜版.md @@ -278,6 +278,39 @@ class Solution: return result ``` +### JavaScript + +```javascript +var numIslands = function (grid) { + let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + + let dfs = (grid, visited, x, y) => { + for (let i = 0; i < 4; i++) { + let nextX = x + dir[i][0] + let nextY = y + dir[i][1] + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) + continue; + if (!visited[nextX][nextY] && grid[nextX][nextY] === "1") { + visited[nextX][nextY] = true + dfs(grid,visited,nextX,nextY) + } + } + } + let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false)) + + let res = 0 + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (!visited[i][j] && grid[i][j] === "1") { + ++res; + visited[i][j] = true; + dfs(grid, visited, i, j); + } + } + } + return res +}; +```

diff --git a/problems/0695.岛屿的最大面积.md b/problems/0695.岛屿的最大面积.md index 186f044c..894f3a75 100644 --- a/problems/0695.岛屿的最大面积.md +++ b/problems/0695.岛屿的最大面积.md @@ -390,6 +390,44 @@ class Solution: if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): self.dfs(grid, visited, new_x, new_y) ``` +### JavaScript +```javascript +var maxAreaOfIsland = function (grid) { + let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向 + + let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false)) + + let dfs = (grid, visited, x, y, m) => { + for (let i = 0; i < 4; i++) { + let nextX = x + dir[i][0] + let nextY = y + dir[i][1] + if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) + continue; + if (!visited[nextX][nextY] && grid[nextX][nextY] === 1) { + visited[nextX][nextY] = true + m = dfs(grid, visited, nextX, nextY,m+1) + } + } + return m + } + + let max = 0 + + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[i].length; j++) { + if (!visited[i][j] && grid[i][j] === 1) { + // 深度优先 + visited[i][j] = true; + let m = dfs(grid, visited, i, j, 1); + if (m > max) max = m; + } + } + } + return max +}; +``` + +

diff --git a/problems/0797.所有可能的路径.md b/problems/0797.所有可能的路径.md index ec8288c6..6b47c42e 100644 --- a/problems/0797.所有可能的路径.md +++ b/problems/0797.所有可能的路径.md @@ -217,6 +217,28 @@ class Solution: self.path.pop() # 回溯 ``` +### JavaScript +```javascript +var allPathsSourceTarget = function(graph) { + let res=[],path=[] + + function dfs(graph,start){ + if(start===graph.length-1){ + res.push([...path]) + return; + } + for(let i=0;i