lettcode面试题目16.19

题目链接

class Solution:
    def pondSizes(self, land: List[List[int]]) -> List[int]:
        if len(land)==0:return []
        directions=[(-1,0),(1,0),(0,-1),(0,1),(-1,1),(-1,-1),(1,-1),(1,1)]
        res,m,n=[],len(land),len(land[0])
        for i in range(m):
            for j in range(n):
                if land[i][j]==0:
                    queue=[(i,j)]
                    land[i][j]=1
                    cnt=1
                    while queue:
                        next_queue=[]
                        for old_x,old_y in queue:
                            for direction in directions:
                                new_x,new_y=old_x+direction[0],old_y+direction[1]
                                if 0<=new_x<m and 0<=new_y<n and land[new_x][new_y]==0:
                                    land[new_x][new_y]=1
                                    cnt+=1
                                    next_queue.append((new_x,new_y))
                        queue=next_queue
                    res.append(cnt)
        res.sort()
        return res              

BFS计算有多少个8方向连接的岛屿,并且按照岛屿的面积从小到大输出一个列表。

全部评论

相关推荐

看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗?&nbsp;那对老老实实面试的人岂不是不公平....
程序员牛肉:公平那是对小孩子讲的童话故事,成年人的世界只有能不能接受失败的后果。 你要是能接受面试作弊被发现之后多家公司联合永久拉黑的后果,你就搞。
你找工作的时候用AI吗?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
07-04 09:21
已编辑
Java
推拿大师:这是hr发的钓鱼贴吗
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务