class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def dfs(nr, nc, grid):
grid[nr][nc] = '0'
for x,y in [(nr - 1, nc),(nr + 1, nc),(nr, nc + 1),(nr, nc - 1)]:
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == '1':
print(x,y)
dfs(x, y, grid)
if len(grid) == 0:
return 0
row = len(grid)
col = len(grid[0])
res = 0
for nr in range(row):
for nc in range(col):
if grid[nr][nc] == '1':
res += 1
dfs(nr, nc, grid)
return res
05-06
05-06
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交