From f6557d7ddc15aff15dfe4b5dcc956aca98e7324c Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 15:31:33 +0800 Subject: [PATCH 1/2] =?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.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7.\346\267\261\346\220\234\347\211\210.md" | 71 ++++++++----------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index a4b93c3d4e..7431fbefba 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -389,50 +389,41 @@ function numIslands(grid: string[][]): number { ### Go ```go + +var DIRECTIONS = [4][2]int{{-1, 0}, {0, -1}, {1, 0}, {0, 1}} + 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])) - } + res := 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}) - } - } - } - } + visited := make([][]bool, len(grid)) + for i := 0; i < len(grid); i++ { + visited[i] = make([]bool, len(grid[0])) + } - 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++ - } - } - } + for i, rows := range grid { + for j, v := range rows { + if v == '1' && !visited[i][j] { + res++ + dfs(grid, visited, i, j) + } + } + } + + return res +} + +func dfs(grid [][]byte, visited [][]bool, i, j int) { + for _, d := range DIRECTIONS { + x, y := i+d[0], j+d[1] + if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { + continue + } + if grid[x][y] == '1' && !visited[x][y] { + visited[x][y] = true + dfs(grid, visited, x, y) + } + } - return result } ``` From 26c4004d74c2480a96722a563a5dd724cedb1e71 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Thu, 11 Jan 2024 17:40:16 +0800 Subject: [PATCH 2/2] Apply suggestions from code review --- ...225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" index 7431fbefba..18442943ad 100644 --- "a/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" +++ "b/problems/0200.\345\262\233\345\261\277\346\225\260\351\207\217.\346\267\261\346\220\234\347\211\210.md" @@ -413,13 +413,13 @@ func numIslands(grid [][]byte) int { } func dfs(grid [][]byte, visited [][]bool, i, j int) { + visited[x][y] = true for _, d := range DIRECTIONS { x, y := i+d[0], j+d[1] if x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) { continue } if grid[x][y] == '1' && !visited[x][y] { - visited[x][y] = true dfs(grid, visited, x, y) } }