leetcode day 2 【1905. 统计子岛屿】 BFS/DFS

这篇博客探讨了在Python中使用BFS和DFS算法解决网格问题,特别是计算子岛屿数量的场景。文章比较了两种方法在时间和内存消耗上的效率,指出BFS通常具有更好的性能。同时,解释了Python中全局变量和局部变量的使用,提供了两种不同的DFS实现方式,一种通过全局变量self.check,另一种通过参数传递。总结强调了在处理全局变量时的注意事项,以及如何优化函数设计。
摘要由CSDN通过智能技术生成

在这里插入图片描述

解题思路

  • BFS

找到grid2中的每一座岛屿[暴力搜索整个grid矩阵],对每座岛屿BFS,过程中check岛屿格子是否在grid1中为岛屿。

class Solution:
    def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:

        row, col = len(grid2), len(grid2[0])
        def BFS(x:int, y:int) -> int:
            grid2[x][y]  = 0
            q = deque()
            q.append((x, y))
            check = (grid1[x][y] == 1)
            while(q):
                x, y = q.popleft()
                for nx ,ny in [(x-1,y),(x,y-1),(x+1,y),(x,y+1)]:
                    if 0<=nx<row and 0<=ny<col:
                        if grid2[nx][ny] == 1:
                            grid2[nx][ny] = 0
                            q.append((nx,ny))
                            if grid1[nx][ny] == 0:
                                check = False
            return check
            
        count = 0
        for i in range(row):
            for j in range(col):
                if grid2[i][j] == 1:
                    count = count + int(BFS(i,j))
        return count
  • DFS

DFS第一种:找到grid2中的每一座岛屿[暴力搜索整个grid矩阵],对每座岛屿DFS,过程中check岛屿格子是否在grid1中为岛屿。【这里用self.check作为全局变量,DFS中对其修改,无返回值】

class Solution:
    def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:

        row, col = len(grid2), len(grid2[0])
        self.check = True
        def DFS(x:int, y:int) -> int:
            for nx ,ny in [(x-1,y),(x,y-1),(x+1,y),(x,y+1)]:
                if 0<=nx<row and 0<=ny<col and grid2[nx][ny] == 1:
                    grid2[nx][ny] = 0
                    if grid1[nx][ny] == 0:
                        self.check = False
                    DFS(nx,ny)
            return

        count = 0
        for i in range(row):
            for j in range(col):
                if grid2[i][j] == 1:
                    self.check = (grid1[i][j] == 1)
                    grid2[i][j] = 0
                    DFS(i,j)
                    count = count + int(self.check)
        return count

DFS第二种:【这里用将check作为参数传入DFS中,并作为返回值】。这种方法其实不太好,因为每个递归DFS中都要开辟一个新的叫check的局部变量空间,这是没必要的。

class Solution:
    def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:

        row, col = len(grid2), len(grid2[0])
        def DFS(x:int, y:int, check:bool) -> int:
            for nx ,ny in [(x-1,y),(x,y-1),(x+1,y),(x,y+1)]:
                if 0<=nx<row and 0<=ny<col and grid2[nx][ny] == 1:
                    grid2[nx][ny] = 0
                    if grid1[nx][ny] == 0:
                        check = False
                    check = DFS(nx,ny,check)
            return check

        count = 0
        for i in range(row):
            for j in range(col):
                if grid2[i][j] == 1:
                    check = (grid1[i][j] == 1)
                    grid2[i][j] = 0
                    checko  = DFS(i,j,check)
                    count = count + int(checko)
        return count

从计算时间和内存消耗来看,还是BFS的效果好。
在这里插入图片描述

总结

  • 1.python的全局变量、局部变量

很有讲究,在函数内部一般是无法修改全局变量的
在这里插入图片描述

  • 解决方法是:

【推荐】①用self.check作为可修改的全局变量:详见上面DFS第一种写法
②通过参数传入DFS函数:详见上面DFS第二种写法


python中函数的定义,调用,全局变量,局部变量,函数的嵌套使用-初级篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值