Skip to content

Instantly share code, notes, and snippets.

@Ian729
Created January 17, 2022 12:49
Show Gist options
  • Save Ian729/846fe94dcf5f7969f6d92ed77b8254b4 to your computer and use it in GitHub Desktop.
Save Ian729/846fe94dcf5f7969f6d92ed77b8254b4 to your computer and use it in GitHub Desktop.
Depth First Search
# Leetcode 200
# https://leetcode.com/problems/number-of-islands/
def dfs(grid, i, j):
if grid[i][j] == "1":
grid[i][j] = "0"
if i-1 >= 0:
dfs(grid, i-1, j)
if i+1 < len(grid):
dfs(grid, i+1, j)
if j-1 >= 0:
dfs(grid, i, j-1)
if j+1 < len(grid[0]):
dfs(grid, i, j+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment