public int numIslands(char[][] grid) { if (grid.length == 0 || grid[0].length == 0) return 0; m = grid.length; n = grid[0].length; int count = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '1') { dfs(grid, i, j); count++; } } } return count; }
public void dfs(char[][] grid, int i, int j) { if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == '0') return; grid[i][j] = '0'; dfs(grid, i-1, j); dfs(grid, i+1, j); dfs(grid, i, j-1); dfs(grid, i, j+1); }
If count is outside dfs, i.e. outside inner for loop, then each row would make count++: a count == grid.length: meaningless, not dfs.
public int numIslands(char[][] grid) { if (grid.length == 0 || grid[0].length == 0) return 0; m = grid.length; n = grid[0].length; int count = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == '1') { dfs(grid, i, j); } count++; } } return count; }
public void dfs(char[][] grid, int i, int j) { if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == '0') return; grid[i][j] = '0'; dfs(grid, i-1, j); dfs(grid, i+1, j); dfs(grid, i, j-1); dfs(grid, i, j+1); }