diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md index d43d6183..f70ea50c 100644 --- a/problems/0200.岛屿数量.广搜版.md +++ b/problems/0200.岛屿数量.广搜版.md @@ -144,3 +144,50 @@ public: }; ``` + +## 其他语言版本 + +### Java + +```java +class Solution { + + boolean[][] visited; + int[][] move = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + + public int numIslands(char[][] grid) { + int res = 0; + visited = new boolean[grid.length][grid[0].length]; + for(int i = 0; i < grid.length; i++) { + for(int j = 0; j < grid[0].length; j++) { + if(!visited[i][j] && grid[i][j] == '1') { + bfs(grid, i, j); + res++; + } + } + } + return res; + } + + //将这片岛屿上的所有陆地都访问到 + public void bfs(char[][] grid, int y, int x) { + Deque queue = new ArrayDeque<>(); + queue.offer(new int[]{y, x}); + visited[y][x] = true; + while(!queue.isEmpty()) { + int[] cur = queue.poll(); + int m = cur[0]; + int n = cur[1]; + for(int i = 0; i < 4; i++) { + int nexty = m + move[i][0]; + int nextx = n + move[i][1]; + if(nextx < 0 || nexty == grid.length || nexty < 0 || nextx == grid[0].length) continue; + if(!visited[nexty][nextx] && grid[nexty][nextx] == '1') { + queue.offer(new int[]{nexty, nextx}); + visited[nexty][nextx] = true; //只要加入队列就标记为访问 + } + } + } + } +} +```