Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def numIslands(self, grid: List[List[str]]) -> int:
- numIslands = 0
- def dfs(y, x):
- if y < 0 or y > len(grid)-1 or x < 0 or x > len(grid[y])-1:
- return
- if grid[y][x] == "1":
- grid[y][x] = "#"
- directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]
- for up, down in directions:
- dfs(y+up, x+down)
- for y in range(len(grid)):
- for x in range(len(grid[y])):
- if grid[y][x] == "1":
- dfs(y, x)
- numIslands += 1
- return numIslands
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement