Untitled
unknown
plain_text
3 years ago
668 B
12
Indexable
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
num_rows = len(grid)
num_cols = len(grid[0])
count=0
moves=[[-1,0],[1,0],[0,1],[0,-1]]
def dfs(x,y):
grid[x][y]="0"
for dx,dy in moves:
tempX=x+dx
tempY=y+dy
if 0<=tempX<num_rows and 0<=tempY<num_cols and grid[tempX][tempY]=='1':
dfs(tempX,tempY)
for i in range(num_rows):
for j in range(num_cols):
if grid[i][j]=='1':
dfs(i,j)
count+=1
return count
Editor is loading...