200. Number of Islands

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:

11110
11010
11000
00000
Answer: 1

Example 2:

11000
11000
00100
00011
Answer: 3

This is a classic dfs search question.

public class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        if(grid.length==0 || grid[0].length == 0) return count;
        boolean[][] visited = new boolean[grid.length][grid[0].length];
        for(int i=0; i< visited.length;i++){
            for(int j=0; j< visited[0].length; j++){
                visited[i][j] = false;
            }
        }
        for(int i=0; i< grid.length; i++){
            for(int j=0; j< grid[0].length;j++){
                if(grid[i][j] == '1' && !visited[i][j]){
                    count++;
                    dfs(grid, visited, i,j);
                }
            }
        }
        return count;
    }

    private void dfs(char[][] grid, boolean[][] visited, int i, int j){
        if(i<0 || i>= grid.length || j <0 || j>=grid[0].length || grid[i][j] == '0' || visited[i][j]){
            return;
        }
        visited[i][j] = true;
        dfs(grid, visited, i,j+1);
        dfs(grid, visited, i,j-1);
        dfs(grid, visited, i+1,j);
        dfs(grid, visited, i-1,j);
    }
}

you can avoid the visited matrix by set the value of grid[i][j] to '2' for instance

results matching ""

    No results matching ""