# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 判断岛屿数量 # @param grid char字符型二维数组 # @return int整型 # class Solution: def solve(self , grid: List[List[str]]) -> int: # write code here width = len(grid) height = len(grid[0]) lands = [] for i in range(width): for j in range(height): if grid[i][j] == "1": lands.append((i, j)) count = 0 while lands: queue = lands.pop(0) if grid[queue[0]][queue[1]] == "2": continue queue = [queue] while queue: (i, j) = queue.pop(0) for (a, b) in [(0, 1), (0, -1), (1, 0), (-1, 0)]: i_s = i + a j_s = j + b if 0 <= i_s < width and 0 <= j_s < height and grid[i_s][j_s] == "1": queue.append((i_s, j_s)) grid[i_s][j_s] = "2" count += 1 return count