题解 | #岛屿的最大面积#
岛屿的最大面积
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