思路
一个陆地的周长为4
如果这个陆地上、下、左、右有陆地相连,则这个岛屿的周长=4-相邻陆地的边的长度
class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
row=len(grid)
col=len(grid[0])
area=0
for i in range(row):
for j in range(col):
if grid[i][j]==1:
t=4
if i-1>=0 and grid[i-1][j]==1:
t-=1
if i+1<row and grid[i+1][j]==1:
t-=1
if j-1>=0 and grid[i][j-1]==1:
t-=1
if j+1<col and grid[i][j+1]==1:
t-=1
area+=t
return area
标签:陆地,周长,int,List,岛屿,463,grid,row
From: https://blog.csdn.net/huanxianxianshi/article/details/142422826