不会,暂时先抄一下。
主要是四领域置零直至领域无1,递归
class Solution: def numIslands(self, grid: List[List[str]]) -> int: counter = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] > '0': counter += 1 self.dfs(grid, i, j) return counter def dfs(self, grid, i, j): if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == '0': return grid[i][j] = '0' self.dfs(grid, i, j-1) self.dfs(grid, i+1, j) self.dfs(grid, i, j+1) self.dfs(grid, i-1, j)