Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Number of Islands problem solution

YASH PAL, 31 July 202419 January 2026

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

Leetcode Number of Islands 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

Number of Islands 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;
  
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes