200. Number of Islands
All prompts are owned by LeetCode. To view the prompt, click the title link above.
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 %
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