如题 leetcode695题当中我用两次循环来遍历grid当中的数字
for x, i in enumerate(grid):
for y, j in enumerate(i):
然后在后续使用过程中,发现grid[x][y]和j的值在特定情况下出现不同
特定的测试用例是
[[1,0,1],[1,0,0],[0,0,1]]
会导致下标为[1][2]的元素在使用j值的时候被误判
请问有没有大 佬能帮我看看 淦
跪 谢!
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
que = collections.deque()
ans = 0
area = 0
for x, i in enumerate(grid):
for y, j in enumerate(i):
if grid[x][y]: # 这里好像不能用j为什么?
que.append((x, y))
grid[x][y] = 0
area = 1
while que:
x, y = que.popleft()
for mx, ny in [(x, y-1), (x, y+1), (x-1, y), (x+1, y)]:
if 0 <= mx < m and 0 <= ny < n and grid[mx][ny] == 1:
area += 1
que.append((mx, ny))
grid[mx][ny] = 0
ans = max(ans, area)
return ans