In this Leetcode Number of Islands problem solution we have Given an m x n 2D binary grid which represents a map of '1's (land) and '0's (water), return 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.

Leetcode Number of Islands problem solution


Problem solution in Python.

def numIslands(self, grid):
        def dfs(x, y):
            if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] != '1':
                return
            grid[x][y] = '0'
            for i, j in [(x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)]:
                dfs(i, j)
        
        if not grid or not grid[0]:
            return 0
        res = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == '1':
                    dfs(i, j)
                    res += 1
        return res



Problem solution in Java.

class Solution {
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0)
            return 0;
        int r = grid.length;
        int c = grid[0].length;
        int res = 0;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }
    
    private void dfs(char[][] grid, int r, int c) {
        int rowNum = grid.length;
        int colNum = grid[0].length;
        if (r < 0 || r >= rowNum || c < 0 || c >= colNum || grid[r][c] == '0') {
            return;
        }
        grid[r][c] = '0';
        dfs(grid, r + 1, c);
        dfs(grid, r - 1, c);
        dfs(grid, r, c + 1);
        dfs(grid, r, c - 1);
    }
}


Problem solution in C++.

class Solution {
public:
    int row, col;
    void dfs(vector<vector<char>> & grid, int y, int x){
        if(y < 0 || x < 0 || y >= row || x >= col || grid[y][x] != '1')
            return ;
        grid[y][x] = '#';
        dfs(grid, y + 1, x);
        dfs(grid, y - 1, x);
        dfs(grid, y, x - 1);
        dfs(grid, y, x + 1);
    }
    int numIslands(vector<vector<char>>& grid) {
        row = grid.size();
        if(!row) return 0;
        col = grid[0].size();
        int count = 0;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(grid[i][j] == '1'){
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
};


Problem solution in C.

int label(char** grid,int row,int col,int gridRowSize, int gridColSize );
int numIslands(char** grid, int gridRowSize, int gridColSize) {
    int row,col;
    int newlabel;
    newlabel=0;
    for(row=0;row<=gridRowSize-1;row++)
    {
       for(col=0;col<=gridColSize-1;col++) 
       {
           newlabel=newlabel+label(grid,row,col,gridRowSize,gridColSize);
       }
        
    }
    
    return newlabel;
}

int label(char** grid,int row,int col,int gridRowSize, int gridColSize )
{
    if (*(*(grid+row)+col)=='1')
    {
        *(*(grid+row)+col)='0';
        if (row!=0)
            label(grid,row-1,col,gridRowSize,gridColSize );
        if (row!=gridRowSize-1)    
            label(grid,row+1,col,gridRowSize,gridColSize );
        if (col!=0)
            label(grid,row,col-1,gridRowSize,gridColSize );
        if (col!=gridColSize-1)    
            label(grid,row,col+1,gridRowSize,gridColSize ); 
        
        return 1;
    }
    else    
        return 0;
  
}