题解 | #岛屿数量#python3 普通的递归dfs

岛屿数量

http://www.nowcoder.com/practice/0c9664d1554e466aa107d899418e814e

#坑就是grid里的元素时字符串形式..
class Solution:
    def solve(self , grid ):
        # write code here
        record = [[0 for e in sublist] for sublist in grid]
        c = 0
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                print(record[i][j],  grid[i][j])
                if record[i][j] == 0 and grid[i][j] == '1':
                    self.dfs(grid, record, i, j)
                    c += 1
        return c

    def dfs(self, grid, record, i, j):
        record[i][j] = 1
        for x, y in [[i-1, j], [i+1, j], [i, j-1], [i, j+1]]:
            if x < len(grid) and y < len(grid[0]) and x >=0 and y >=0 and record[x][y] == 0 and grid[x][y] == '1':
                self.dfs(grid, record, x, y)
全部评论

相关推荐

死在JAVA的王小美:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈,我也是,让我免了一轮,但是硬气拒绝了
点赞 评论 收藏
分享
已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务