diff --git "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" index d43d6183f0..f70ea50c89 100644 --- "a/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.md" +++ "b/problems/0200.345円262円233円345円261円277円346円225円260円351円207円217円.345円271円277円346円220円234円347円211円210円.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; //只要加入队列就标记为访问 + } + } + } + } +} +``` diff --git "a/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" "b/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" index e1f43e36b7..447a374c48 100644 --- "a/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" +++ "b/problems/0695.345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.md" @@ -170,6 +170,81 @@ public: # 其它语言版本 +## Python +### BFS +```python +class Solution: + def __init__(self): + self.count = 0 + + def maxAreaOfIsland(self, grid: List[List[int]]) -> int: + # 与200.独立岛屿不同的是:此题grid列表内是int!!! + + # BFS + if not grid: return 0 + + m, n = len(grid), len(grid[0]) + visited = [[False for i in range(n)] for j in range(m)] + + result = 0 + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == 1: + # 每一个新岛屿 + self.count = 0 + print(f'{self.count}') + self.bfs(grid, visited, i, j) + result = max(result, self.count) + + return result + + def bfs(self, grid, visited, i, j): + self.count += 1 + visited[i][j] = True + + queue = collections.deque([(i, j)]) + while queue: + x, y = queue.popleft() + for new_x, new_y in [(x + 1, y), (x - 1, y), (x, y - 1), (x, y + 1)]: + if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]) and not visited[new_x][new_y] and grid[new_x][new_y] == 1: + visited[new_x][new_y] = True + self.count += 1 + queue.append((new_x, new_y)) +``` +### DFS +```python +class Solution: + def __init__(self): + self.count = 0 + + def maxAreaOfIsland(self, grid: List[List[int]]) -> int: + # DFS + if not grid: return 0 + + m, n = len(grid), len(grid[0]) + visited = [[False for _ in range(n)] for _ in range(m)] + + result = 0 + for i in range(m): + for j in range(n): + if not visited[i][j] and grid[i][j] == 1: + self.count = 0 + self.dfs(grid, visited, i, j) + result = max(result, self.count) + return result + + def dfs(self, grid, visited, x, y): + if visited[x][y] or grid[x][y] == 0: + return + visited[x][y] = True + self.count += 1 + for new_x, new_y in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]: + if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[0]): + self.dfs(grid, visited, new_x, new_y) +``` + + + ## Java 这里使用深度优先搜索 DFS 来完成本道题目。我们使用 DFS 计算一个岛屿的面积,同时维护计算过的最大的岛屿面积。同时,为了避免对岛屿重复计算,我们在 DFS 的时候对岛屿进行 "淹没" 操作,即将岛屿所占的地方置为 0。 @@ -199,4 +274,4 @@ public int dfs(int[][] grid,int i,int j){ dfs(grid,i,j + 1) + dfs(grid,i,j - 1); } -``` \ No newline at end of file +```

AltStyle によって変換されたページ (->オリジナル) /