diff --git a/solution/200/main.go b/solution/200/main.go new file mode 100644 index 0000000..cf7b0ac --- /dev/null +++ b/solution/200/main.go @@ -0,0 +1,34 @@ +package main + +func numIslands(grid [][]byte) int { + if len(grid) == 0 || len(grid[0]) == 0 { + return 0 + } + + rows := len(grid) + cols := len(grid[0]) + islands := 0 + + var dfs func(row, col int) + dfs = func(row, col int) { + if row < 0 || col < 0 || row >= rows || col >= cols || grid[row][col] != '1' { + return + } + grid[row][col] = '0' + dfs(row-1, col) + dfs(row+1, col) + dfs(row, col-1) + dfs(row, col+1) + } + + for row := 0; row < rows; row++ { + for col := 0; col < cols; col++ { + if grid[row][col] == '1' { + dfs(row, col) + islands++ + } + } + } + + return islands +}