Leetcode 每日一题
题目链接: 463. 岛屿的周长
难度: 简单
解题思路: 对于每一点,查看它的四周,判断他的外围是否有点。若有点则周长减一。
题解:
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
if len(grid) == 0:
return 0
n, m = len(grid), len(grid[0])
circle = 0
for i in range(n):
for j in range(m):
if grid[i][j] == 1:
cnt = 4
if i > 0 and grid[i - 1][j] == 1:
cnt -= 1
if j > 0 and grid[i][j - 1] == 1:
cnt -= 1
if i < n - 1 and grid[i + 1][j] == 1:
cnt -= 1
if j < m - 1 and grid[i][j + 1] == 1:
cnt -= 1
circle += cnt
# print(cnt)
# print(circle)
return circle