leetcode-python-岛屿数量

不会,暂时先抄一下。

主要是四领域置零直至领域无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)

 

上一篇:john the ripper跑多核多cpu


下一篇:【python自动运维】python自动定时监控MSSQL数据库服务启动状态挂掉自动启动服务