给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。
示例 :
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
答案: 16
解释: 它的周长是下面图片中的 16 个黄色的边:
思路:
遍历出所有的1,然后判断周围4边任意一边为0则加1
class Solution:
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
result = 0
leni = len(grid)
lenj = len(grid[0])
for i in range(leni):
for j in range(lenj):
if grid[i][j] == 1:
if i == 0 or grid[i - 1][j] == 0: result += 1
if j == 0 or grid[i][j - 1] == 0: result += 1
if i == leni - 1 or grid[i + 1][j] == 0: result += 1
if j == lenj - 1 or grid[i][j + 1] == 0: result += 1
return result