class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
from collections import deque
def bfs(x, y, n, m):
grid[x][y]=0
queue=deque([(x,y)])
num=1
while queue:
node=queue.popleft()
for (a, b) in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
x1 = node[0] + a
y1 = node[1] + b
if 0 <= x1 < n and 0 <= y1 < m and grid[x1][y1] != 0:
num+=1
grid[x1][y1]=0
queue.append((x1,y1))
return num
n = len(grid)
m = len(grid[0])
maxnums =0
for i in range(n):
for j in range(m):
if grid[i][j] != 0:
maxnums = max(maxnums, bfs(i, j, n, m))
if maxnums== 0:
return null
else:
return maxnums
初始定义queue
queue=collections.deque([node])
queue.append(node)
node可以以点的形式
如queue=deque([(x,y)])
之后加进去的坐标都以
queue.append((x1,y1)) 这种形式
由于该题已经进行涂色环节,故不用再进行哈希表去重。