Created
April 18, 2020 05:17
-
-
Save vamsitallapudi/5e7ddcde9cab4309f9da5fe73612496e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def numIslands(self, grid): | |
if not grid: | |
return 0 | |
count = 0 | |
for i in range(len(grid)): | |
for j in range(len(grid[0])): | |
if grid[i][j] == '1': | |
self.dfs(grid, i, j) | |
count += 1 | |
return count | |
def dfs(self, grid, i, j): | |
if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j] != '1': | |
return | |
grid[i][j] = '#' | |
self.dfs(grid, i+1, j) | |
self.dfs(grid, i-1, j) | |
self.dfs(grid, i, j+1) | |
self.dfs(grid, i, j-1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment