|
49 | 49 | <li><code>grid[i][j]</code> 的值为 <code>'0'</code> 或 <code>'1'</code></li>
|
50 | 50 | </ul>
|
51 | 51 |
|
52 |
| - |
53 | 52 | ## 解法
|
54 | 53 |
|
55 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
56 | 55 |
|
57 |
| -BFS/DFS 均可 |
| 56 | +BFS、DFS、并查集均可。 |
58 | 57 |
|
59 | 58 | <!-- tabs:start -->
|
60 | 59 |
|
61 | 60 | ### **Python3**
|
62 | 61 |
|
63 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
64 | 63 |
|
65 |
| -```python |
| 64 | +并查集: |
66 | 65 |
|
| 66 | +```python |
| 67 | +class Solution: |
| 68 | + def numIslands(self, grid: List[List[str]]) -> int: |
| 69 | + m, n = len(grid), len(grid[0]) |
| 70 | + p = [-1] * (m * n) |
| 71 | + |
| 72 | + def find(x): |
| 73 | + if p[x] != x: |
| 74 | + p[x] = find(p[x]) |
| 75 | + return p[x] |
| 76 | + |
| 77 | + for i in range(m): |
| 78 | + for j in range(n): |
| 79 | + if grid[i][j] == '1': |
| 80 | + p[i * n + j] = i * n + j |
| 81 | + |
| 82 | + for i in range(m): |
| 83 | + for j in range(n): |
| 84 | + if grid[i][j] == '1': |
| 85 | + if i < m - 1 and grid[i + 1][j] == '1': |
| 86 | + a, b = i * n + j, (i + 1) * n + j |
| 87 | + p[find(a)] = find(b) |
| 88 | + if j < n - 1 and grid[i][j + 1] == '1': |
| 89 | + a, b = i * n + j, i * n + j + 1 |
| 90 | + p[find(a)] = find(b) |
| 91 | + |
| 92 | + cnt = 0 |
| 93 | + for i in range(m): |
| 94 | + for j in range(n): |
| 95 | + if i * n + j == find(i * n + j): |
| 96 | + cnt += 1 |
| 97 | + return cnt |
67 | 98 | ```
|
68 | 99 |
|
69 | 100 | ### **Java**
|
70 | 101 |
|
71 | 102 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
72 | 103 |
|
| 104 | +DFS: |
| 105 | + |
73 | 106 | ```java
|
| 107 | +class Solution { |
| 108 | + public int numIslands(char[][] grid) { |
| 109 | + int islandNum = 0; |
| 110 | + for (int i = 0; i < grid.length; i++) { |
| 111 | + for (int j = 0; j < grid[0].length; j++) { |
| 112 | + if (grid[i][j] == '1') { |
| 113 | + infect(grid, i, j); |
| 114 | + islandNum ++; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return islandNum; |
| 119 | + } |
74 | 120 |
|
| 121 | + public void infect(char[][] grid, int i, int j) { |
| 122 | + if (i < 0 || i >= grid.length || |
| 123 | + j < 0 || j >= grid[0].length || |
| 124 | + grid[i][j] != '1') { |
| 125 | + return; |
| 126 | + } |
| 127 | + grid[i][j] = '2'; |
| 128 | + infect(grid, i + 1, j); |
| 129 | + infect(grid, i - 1, j); |
| 130 | + infect(grid, i, j + 1); |
| 131 | + infect(grid, i, j - 1); |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +并查集: |
| 137 | + |
| 138 | +```java |
| 139 | +class Solution { |
| 140 | + private int[] p; |
| 141 | + |
| 142 | + public int numIslands(char[][] grid) { |
| 143 | + int m = grid.length, n = grid[0].length; |
| 144 | + p = new int[m * n]; |
| 145 | + for (int i = 0; i < m; ++i) { |
| 146 | + for (int j = 0; j < n; ++j) { |
| 147 | + p[i * n + j] = grid[i][j] == '1' ? i * n + j : -1; |
| 148 | + } |
| 149 | + } |
| 150 | + for (int i = 0; i < m; ++i) { |
| 151 | + for (int j = 0; j < n; ++j) { |
| 152 | + if (grid[i][j] == '1') { |
| 153 | + if (i < m - 1 && grid[i + 1][j] == '1') { |
| 154 | + p[find(i * n + j)] = find((i + 1) * n + j); |
| 155 | + } |
| 156 | + if (j < n - 1 && grid[i][j + 1] == '1') { |
| 157 | + p[find(i * n + j)] = find(i * n + j + 1); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + int cnt = 0; |
| 163 | + for (int i = 0; i < m; ++i) { |
| 164 | + for (int j = 0; j < n; ++j) { |
| 165 | + if (p[i * n + j] != -1 && i * n + j == find(i * n + j)) { |
| 166 | + ++cnt; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + return cnt; |
| 171 | + } |
| 172 | + |
| 173 | + private int find(int x) { |
| 174 | + if (p[x] != x) { |
| 175 | + p[x] = find(p[x]); |
| 176 | + } |
| 177 | + return p[x]; |
| 178 | + } |
| 179 | +} |
75 | 180 | ```
|
76 | 181 |
|
77 | 182 | ### **TypeScript**
|
78 | 183 |
|
79 | 184 | ```ts
|
80 | 185 | function numIslands(grid: string[][]): number {
|
81 |
| - let m = grid.length, n = grid[0].length; |
82 |
| - let ans = 0; |
83 |
| - for (let i = 0; i < m; ++i) { |
84 |
| - for (let j = 0; j < n; ++j) { |
85 |
| - if (grid[i][j] == '1') { |
86 |
| - dfs(grid, i, j); |
87 |
| - ++ans; |
88 |
| - } |
89 |
| - } |
| 186 | + let m = grid.length, |
| 187 | + n = grid[0].length; |
| 188 | + let ans = 0; |
| 189 | + for (let i = 0; i < m; ++i) { |
| 190 | + for (let j = 0; j < n; ++j) { |
| 191 | + if (grid[i][j] == "1") { |
| 192 | + dfs(grid, i, j); |
| 193 | + ++ans; |
| 194 | + } |
90 | 195 | }
|
91 |
| - return ans; |
92 |
| -}; |
| 196 | + } |
| 197 | + return ans; |
| 198 | +} |
93 | 199 |
|
94 | 200 | function dfs(grid: string[][], i: number, j: number) {
|
95 |
| - let m = grid.length, n = grid[0].length; |
96 |
| - if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || grid[i][j] == '0') { |
97 |
| - return; |
| 201 | + let m = grid.length, |
| 202 | + n = grid[0].length; |
| 203 | + if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || grid[i][j] == "0") { |
| 204 | + return; |
| 205 | + } |
| 206 | + grid[i][j] = "0"; |
| 207 | + for (let [dx, dy] of [ |
| 208 | + [0, 1], |
| 209 | + [0, -1], |
| 210 | + [1, 0], |
| 211 | + [-1, 0], |
| 212 | + ]) { |
| 213 | + let x = i + dx, |
| 214 | + y = j + dy; |
| 215 | + dfs(grid, x, y); |
| 216 | + } |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +### **C++** |
| 221 | + |
| 222 | +```cpp |
| 223 | +class Solution { |
| 224 | +public: |
| 225 | + vector<int> p; |
| 226 | + |
| 227 | + int numIslands(vector<vector<char>> &grid) { |
| 228 | + int m = grid.size(), n = grid[0].size(); |
| 229 | + p.resize(m * n); |
| 230 | + for (int i = 0; i < m; ++i) |
| 231 | + { |
| 232 | + for (int j = 0; j < n; ++j) |
| 233 | + { |
| 234 | + p[i * n + j] = grid[i][j] == '1' ? i * n + j : -1; |
| 235 | + } |
| 236 | + } |
| 237 | + for (int i = 0; i < m; ++i) |
| 238 | + { |
| 239 | + for (int j = 0; j < n; ++j) |
| 240 | + { |
| 241 | + if (grid[i][j] == '1') |
| 242 | + { |
| 243 | + if (i < m - 1 && grid[i + 1][j] == '1') |
| 244 | + { |
| 245 | + p[find(i * n + j)] = find((i + 1) * n + j); |
| 246 | + } |
| 247 | + if (j < n - 1 && grid[i][j + 1] == '1') |
| 248 | + { |
| 249 | + p[find(i * n + j)] = find(i * n + j + 1); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + int cnt = 0; |
| 255 | + for (int i = 0; i < m; ++i) |
| 256 | + { |
| 257 | + for (int j = 0; j < n; ++j) |
| 258 | + { |
| 259 | + if (p[i * n + j] != -1 && i * n + j == find(i * n + j)) |
| 260 | + ++cnt; |
| 261 | + } |
| 262 | + } |
| 263 | + return cnt; |
98 | 264 | }
|
99 |
| - grid[i][j] = '0'; |
100 |
| - for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) { |
101 |
| - let x = i + dx, y = j + dy; |
102 |
| - dfs(grid, x, y); |
| 265 | + |
| 266 | + int find(int x) { |
| 267 | + if (p[x] != x) |
| 268 | + { |
| 269 | + p[x] = find(p[x]); |
| 270 | + } |
| 271 | + return p[x]; |
103 | 272 | }
|
| 273 | +}; |
| 274 | +``` |
| 275 | + |
| 276 | +### **Go** |
| 277 | + |
| 278 | +```go |
| 279 | +var p []int |
| 280 | + |
| 281 | +func numIslands(grid [][]byte) int { |
| 282 | + m, n := len(grid), len(grid[0]) |
| 283 | + p = make([]int, m*n) |
| 284 | + for i := 0; i < m; i++ { |
| 285 | + for j := 0; j < n; j++ { |
| 286 | + if grid[i][j] == '1' { |
| 287 | + p[i*n+j] = i*n + j |
| 288 | + } else { |
| 289 | + p[i*n+j] = -1 |
| 290 | + } |
| 291 | + } |
| 292 | + } |
| 293 | + for i := 0; i < m; i++ { |
| 294 | + for j := 0; j < n; j++ { |
| 295 | + if grid[i][j] == '1' { |
| 296 | + if i < m-1 && grid[i+1][j] == '1' { |
| 297 | + p[find(i*n+j)] = find((i+1)*n + j) |
| 298 | + } |
| 299 | + if j < n-1 && grid[i][j+1] == '1' { |
| 300 | + p[find(i*n+j)] = find(i*n + j + 1) |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + cnt := 0 |
| 306 | + for i := 0; i < m; i++ { |
| 307 | + for j := 0; j < n; j++ { |
| 308 | + if p[i*n+j] != -1 && i*n+j == find(i*n+j) { |
| 309 | + cnt++ |
| 310 | + } |
| 311 | + } |
| 312 | + } |
| 313 | + return cnt |
| 314 | +} |
| 315 | + |
| 316 | +func find(x int) int { |
| 317 | + if p[x] != x { |
| 318 | + p[x] = find(p[x]) |
| 319 | + } |
| 320 | + return p[x] |
104 | 321 | }
|
105 | 322 | ```
|
106 | 323 |
|
|
0 commit comments