题解 | #岛屿数量#
岛屿数量
http://www.nowcoder.com/practice/0c9664d1554e466aa107d899418e814e
dfs,走过的标0
# # 判断岛屿数量 # @param grid char字符型二维数组 # @return int整型 # class Solution: def solve(self , grid ): # write code here m = len(grid) if m == 0: return 0 n = len(grid[0]) if n == 0: return 0 def dfs(i, j): if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]): return 0 if grid[i][j] == '0': return 0 grid[i][j] = '0' up_i, up_j = i-1, j dfs(i-1, j) dfs(i+1, j) dfs(i, j-1) dfs(i, j+1) return 1 def display(): for i in grid: print(i) res = 0 for i in range(m): for j in range(n): res += dfs(i, j) # print(i, j) # display() return res