题解 | #岛屿的最大面积#

岛屿的最大面积

https://www.nowcoder.com/practice/5568943d3a08403f932a5e54ec3ece71


class Solution:
    def maxAreaIsland(self , grid) :
        n=len(grid)
        m=len(grid[0])
        result=0
        for i in range(n):
            for j in range(m):
                if grid[i][j]==1:
                    grid[i][j]=0#自己也得改成0,第一次结果没对就错在这了
                    s=1
                    t=[[i,j]]
                    while t:
                        nt=[]
                        for c in t:
                            for nx,ny in(0,1),(0,-1),(1,0),(-1,0):
                                x,y=c[0]+nx,c[1]+ny
                                if n>x>=0 and m>y>=0 and grid[x][y]==1:#如果没越界且为1
                                    nt.append([x,y])
                                    grid[x][y]=0
                                    s+=1
                        t=nt
                        if s>result:
                            result=s
        return result


        # write code here

全部评论

相关推荐

07-22 11:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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