0
我在編碼參加比賽,我得到了超接近解決以下問題,但我由於某種原因,代碼不工作=(最大面積
這裏是鏈接的問題: https://leetcode.com/contest/leetcode-weekly-contest-53/problems/max-area-of-island/
我的解決辦法:
class Solution(object):
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
self.longest = 0
self.count = 0
row = len(grid)
col = len(grid[0])
for i in range(row):
for j in range(col):
if grid[i][j] == 1:
self.count += 1
current = 1
self.countIsland(i, j, current, grid)
return self.longest
def countIsland(self, k, z, current, grid):
print(str(k) + "," + str(z) + "=" + str(current))
grid[k][z] = -1
if k > 0 and grid[k-1][z] == 1:
return self.countIsland(k-1, z, current+1, grid)
if k < (len(grid)-1) and grid[k+1][z] == 1:
return self.countIsland(k+1, z, current+1, grid)
if z > 0 and grid[k][z - 1] == 1:
return self.countIsland(k, z-1, current+1, grid)
if z < (len(grid[0])-1) and grid[k][z+1] == 1:
return self.countIsland(k, z+1, current+1, grid)
self.longest = max(self.longest, current)
return current
我1要走了,我越來越5而不是6.如果您嘗試在IDE中運行它,我的print語句將表明,遞歸的最後一次調用中,當前值正在重新初始化這不是我想要的。任何想法爲什麼?
謝謝!
您的實施是否正確?我的意思是在這條線上'self.longest = max(self.longest,current)',如果它到達兩個近鄰,該怎麼辦? – gautamaggarwal
請在問題中包含問題文本 - 您提供的鏈接需要註冊。 –