岛屿数量

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:

        def one_to_zero(i, j):
            while i < row and j < col and i >= 0 and j >= 0 and grid[i][j] == '1':
                grid[i][j] = '0'
                one_to_zero(i, j-1)
                one_to_zero(i, j+1)
                one_to_zero(i+1, j)
                one_to_zero(i-1, j)
  
        row = len(grid)
        col = len(grid[0])
        count = 0
        for i in range(row):
            for j in range(col):
                if grid[i][j] == '1':
                    count += 1
                    one_to_zero(i, j)
        return count

原文地址:https://www.cnblogs.com/KbMan/p/14498733.html