题目如下:
Given a 2d grid map of '1'
s (land) and '0'
s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input: 11110 11010 11000 00000 Output: 1
Example 2:
Input: 11000 11000 00100 00011 Output: 3
解答如下:
使用dfs标记岛屿,使得每个岛屿内只有一个1.也就是说对于碰到的任何一个1就进行dfs标记与之相连的为2,最后统计1的个数即可
代码:
class Solution {
private int m, n;
public void dfs(char[][] grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n) return;
if (grid[i][j] == '1') {
grid[i][j] = '2';
dfs(grid, i - 1, j);
dfs(grid, i + 1, j);
dfs(grid, i, j - 1);
dfs(grid, i, j + 1);
}
}
public int numIslands(char[][] grid) {
m = grid.length;
if (m == 0) return 0;
n = grid[0].length;
if (n == 0) return 0;
int ans = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != '1') continue;
ans++;
dfs(grid, i, j);
}
}
return ans;
}
}