每一个陆地单元格的周长为4,当两单元格上下或者左右相邻时,令周长减2,每有一个相邻的方块,边就减少两条,判断好边界就没问题了。
class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
valus=
0;
h=len(grid)
w=len(grid[
0])
for x
in range(h):
for y
in range(w):
if grid[x][y] ==
1:
valus+=
4
if (x >
0 and grid[x-
1][y] ==
1):
valus -=
2
if (y >
0 and grid[x][y-
1] ==
1):
valus -=
2
return valus