Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.39 KB

_200. Number of Islands.md

File metadata and controls

50 lines (36 loc) · 1.39 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 17, 2024

Last updated : June 17, 2024


Related Topics : Array, Depth-First Search, Breadth-First Search, Union Find, Matrix

Acceptance Rate : 60.61 %


Solutions

Python

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        counter = 0


        def fillOutIsland(row: int, col: int) :
            if 0 <= row < len(grid) \
                and 0 <= col < len(grid[0]) \
                and grid[row][col] == '1' :
                grid[row][col] = '0'
                fillOutIsland(row - 1, col)
                fillOutIsland(row + 1, col)
                fillOutIsland(row, col - 1)
                fillOutIsland(row, col + 1)
            
        for row in range(len(grid)) :
            for col in range(len(grid[0])) :
                if grid[row][col] == '1' :
                    counter += 1
                    fillOutIsland(row, col)

        return counter