我试图理解这段代码,所以我放了一个puts @visited.inspect
或一个puts visited.inspect
,但什么都没有显示。代码运行正常,如下所示
grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"]]
def max_area_of_island(grid)
@visited = Set.new
@grid = grid
total_sum = [0]
@m = grid.length
@n = grid[0].length
i = 0
while i < @m
j = 0
while j < @n
if grid[i][j] == 1 && !@visited.include?([i,j])
@visited << [i,j]
puts @visited.inspect
total_sum << bfs([i,j])
end
j += 1
end
i += 1
end
total_sum.max
end
我把一个puts @visited.inspect
和一个puts visited.inspect
代码运行顺利,但它没有打印任何东西。我能做些什么来打印@visited的值,只是为了检查程序正在做什么或...你建议我应该调试这段代码吗?在哪?哪个项目?
1条答案
按热度按时间bfrts1fy1#
您的
grid
变量存储字符串,但在下面,您将grid[i][j]
与整数进行比较。这将永远是假的。没有
bfs
的定义,我无法验证这是否是您的代码的唯一问题。